/[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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 348 by dpavlin, Sat Nov 15 23:52:22 2008 UTC revision 369 by dpavlin, Mon Nov 17 14:37:48 2008 UTC
# Line 1  Line 1 
1  package Frey::Run;  package Frey::Run;
2  use Moose;  use Moose;
3  #extends 'Frey::ClassLoader';  #extends 'Frey::ClassLoader';
4  extends 'Frey::PPI';  extends 'Frey::Action';
5  with 'Frey::Web';  with 'Frey::Web';
 with 'Frey::Config';  
6  with 'Frey::Escape';  with 'Frey::Escape';
7    
8  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
# Line 18  Frey::Run - display required form field Line 17  Frey::Run - display required form field
17  This object will try to run other Moose objects from your application. It  This object will try to run other Moose objects from your application. It
18  will try to invoke C<data>, and C<markup> method on the.  will try to invoke C<data>, and C<markup> method on the.
19    
20    =head1 SEE ALSO
21    
22    L<Frey::Action> which creates form for params
23    
24  =cut  =cut
25    
26    use Moose::Util::TypeConstraints;
27    
28    enum 'Runnable' => qw/data markup sponge/;
29    
30  sub runnable { qw/data markup sponge/ }  sub runnable { qw/data markup sponge/ }
31    
32  has 'class' => (  has 'class' => (
# Line 34  has 'params' => ( Line 41  has 'params' => (
41          default => sub { {} },          default => sub { {} },
42  );  );
43    
44    has 'run' => (
45            is => 'rw',
46            isa => 'Runnable',
47            default => 'markup',
48    );
49    
50  sub html {  sub html {
51          my ( $self ) = @_;          my ( $self ) = @_;
52    
         my $class = $self->class;  
   
         $self->load_class( $class );  
   
         my @required =  
                 grep {  
                         defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } )  
                 }  
                 map {  
                         my $attr = $class->meta->get_attribute($_);  
                         $attr->is_required && $attr;  
                 } $class->meta->get_attribute_list;  
   
         warn "## required = ",dump( map { $_->name } @required ), " for $class";  
   
53          my $html;          my $html;
54          my $values = {};          eval {
55          $values = $self->config($class);                  my $class = $self->class;
56          warn "# $class config = ",dump( $values );                  $self->load_class( $class );
57    
58          if ( @required ) {                  if ( $html = $self->params_form ) {
59                  $html = qq|<h1>$class params</h1><form method="post">|;                          warn "got params form for $class";
60                    } else {
61                  my $a;                          my $o;
62                  my @attrs = map {  $a->{$_}++; $_ } $self->attribute_order;                          $o = $class->new( %{ $self->params } );
63                  push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @required;                          $o->depends if $o->can('depends');
64                  warn "# attrs = ",dump( @attrs );  
65                            if ( $self->run eq 'markup' ) {
66                  foreach my $name ( @attrs ) {                                  warn "## using ",ref($o), "->markup";
67                          my $attr = $class->meta->get_attribute( $name );                                  $html = $o->markup;
68                          my $type = $name =~ m/^pass/ ? 'password' : 'text';                                  warn ">>> markup $class ",length( $html ), " bytes\n";
69                          my $value = '';                          } elsif ( $self->run eq 'sponge' ) {
70                          my $value_html = '';                                  my $data = $o->sponge;
71                          if ( ref($values) eq 'HASH' ) {                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
72                                  $value = $values->{$name};                                  my $rows = $#{ $data->{rows} } + 1;
73                          } elsif ( ref($values) eq 'ARRAY' ) {                                  $rows ||= 'no';
74                                  $value_html = qq|<select name="$name">| . join("\n",                                  $html .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>";
75                                          map {                                  $html .= '<table>';
76                                                  my $v = $_->{$name};                                  $html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
77                                                  qq|<option value="$v">$v</option>|                                  $html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
78                                          } @$values                                  $html .= '</table>';
79                                  ) . qq|</select>|;                          } elsif ( $self->run eq 'data' ) {
80                                    my $data = $o->data;
81                                    $html .= Frey::Dumper->new( data => $data )->markup;
82                                    $html .= '<hr/><span class="frey-popdown">dump<span><code>' . $self->html_dump( $data ) . '</code></span></span>';
83                          } else {                          } else {
84                                  $value = $attr->default( $name ) if $attr->has_default;                                  $html = $self->error( "IGNORE: $class ", $o->dump );
85                          }                          }
86                          $value_html = qq|<input type="$type" name="$name" value="$value">| unless $value_html;                  };
87    
88  #warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 );          };
89                          $html .= qq|<label for="$name">$name</label>| . $value_html;          $html .= $self->error( $@ ) if $@;
                 }  
                 $html .= qq|<input type="submit" value="Run $class"></form>|;  
         } else {  
                 my $o = $class->new( %{ $self->params } );  
                 $o->depends if $o->can('depends');  
                 if ( $o->can('markup') ) {  
                         warn "## using ",ref($o), "->markup";  
                         $html = eval { $o->markup };  
                         if ( $@ ) {  
                                 warn $@;  
                                 $html .= qq{<code>$@</code>};  
                         }  
                         warn ">>> markup $class ",length( $html ), " bytes\n";  
                 } elsif ( $o->can('sponge') ) {  
                         my $data = $o->sponge;  
                         $html .= '<table>';  
                         $html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';  
                         $html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };  
                         $html .= '</table>';  
                 } elsif ( $o->can('data') ) {  
                         my $data = $o->data;  
                         $html .= Frey::Dumper->new( data => $data )->markup;  
                         $html .= '<hr/><code>' . $self->html_dump( $data ) . '</code>';  
                 } else {  
                         $html = "IGNORE: $class ", $o->dump;  
                         warn $html;  
                 }  
         }  
90    
91          return $self->page( title => $class, body => $html );          return $self->page( %$html ) if ref($html) eq 'HASH';
92            return $self->page( title => $self->class, body => $html );
93  }  }
94    
95  1;  1;

Legend:
Removed from v.348  
changed lines
  Added in v.369

  ViewVC Help
Powered by ViewVC 1.1.26