/[Frey]/trunk/lib/Frey/Run.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Frey/Run.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 351 - (show annotations)
Sun Nov 16 13:01:56 2008 UTC (15 years, 5 months ago) by dpavlin
File size: 3771 byte(s)
dispatch which method to invoke to Frey::Run
1 package Frey::Run;
2 use Moose;
3 #extends 'Frey::ClassLoader';
4 extends 'Frey::PPI';
5 with 'Frey::Web';
6 with 'Frey::Config';
7 with 'Frey::Escape';
8
9 use Data::Dump qw/dump/;
10 use Frey::Dumper;
11
12 =head1 NAME
13
14 Frey::Run - display required form field for Class and run it
15
16 =head1 DESCRIPTION
17
18 This object will try to run other Moose objects from your application. It
19 will try to invoke C<data>, and C<markup> method on the.
20
21 =cut
22
23 use Moose::Util::TypeConstraints;
24
25 enum 'Runnable' => qw/data markup sponge/;
26
27 sub runnable { qw/data markup sponge/ }
28
29 has 'class' => (
30 is => 'rw',
31 isa => 'Str',
32 required => 1,
33 );
34
35 has 'params' => (
36 is => 'rw',
37 isa => 'HashRef',
38 default => sub { {} },
39 );
40
41 has 'run' => (
42 is => 'rw',
43 isa => 'Runnable',
44 default => 'markup',
45 );
46
47 sub html {
48 my ( $self ) = @_;
49
50 my $class = $self->class;
51
52 $self->load_class( $class );
53
54 my @required =
55 grep {
56 defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } )
57 }
58 map {
59 my $attr = $class->meta->get_attribute($_);
60 $attr->is_required && $attr;
61 } $class->meta->get_attribute_list;
62
63 warn "## required = ",dump( map { $_->name } @required ), " for $class";
64
65 my $html;
66 my $values = {};
67 $values = $self->config($class);
68 warn "# $class config = ",dump( $values );
69
70 if ( @required ) {
71 $html = qq|<h1>$class params</h1><form method="post">|;
72
73 my $a;
74 my @attrs = map { $a->{$_}++; $_ } $self->attribute_order;
75 push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @required;
76 warn "# attrs = ",dump( @attrs );
77
78 foreach my $name ( @attrs ) {
79 my $attr = $class->meta->get_attribute( $name );
80 my $type = $name =~ m/^pass/ ? 'password' : 'text';
81 my $value = '';
82 my $value_html = '';
83 if ( ref($values) eq 'HASH' ) {
84 $value = $values->{$name};
85 } elsif ( ref($values) eq 'ARRAY' ) {
86 $value_html = qq|<select name="$name">| . join("\n",
87 map {
88 my $v = $_->{$name};
89 qq|<option value="$v">$v</option>|
90 } @$values
91 ) . qq|</select>|;
92 } elsif ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) {
93 $value_html = qq|<select name="$name">| . join("\n",
94 map { qq|<option value="$_">$_</option>| } @{ $attr->type_constraint->values }
95 ) . qq|</select>|;
96 } elsif ( $attr->has_default ) {
97 $value = $attr->default( $name );
98 }
99 $value_html = qq|<input type="$type" name="$name" value="$value">| unless $value_html;
100
101 #warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 );
102 $html .= qq|<label for="$name">$name</label>| . $value_html;
103 }
104 $html .= qq|<input type="submit" value="Run $class"></form>|;
105 } else {
106 my $o;
107 eval { $o = $class->new( %{ $self->params } ); };
108 if ( $@ ) {
109 return $self->page( title => $class, body => $self->error( $@ ) );
110 }
111 $o->depends if $o->can('depends');
112
113 if ( $self->run eq 'markup' ) {
114 warn "## using ",ref($o), "->markup";
115 $html = eval { $o->markup };
116 $html .= $self->error( $@ ) if $@;
117 warn ">>> markup $class ",length( $html ), " bytes\n";
118 } elsif ( $self->run eq 'sponge' ) {
119 my $data = eval { $o->sponge };
120 if ( $@ ) {
121 $html .= $self->error( $@ );
122 } else {
123 $html .= '<table>';
124 $html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
125 $html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
126 $html .= '</table>';
127 }
128 } elsif ( $self->run eq 'data' ) {
129 my $data = eval { $o->data; };
130 if ( $@ ) {
131 $html .= $self->error( $@ );
132 } else {
133 $html .= Frey::Dumper->new( data => $data )->markup;
134 $html .= '<hr/><code>' . $self->html_dump( $data ) . '</code>';
135 }
136 } else {
137 $html = $self->error( "IGNORE: $class ", $o->dump );
138 }
139 }
140
141 return $self->page( title => $class, body => $html );
142 }
143
144 1;

  ViewVC Help
Powered by ViewVC 1.1.26