/[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 349 by dpavlin, Sun Nov 16 00:25:39 2008 UTC revision 400 by dpavlin, Tue Nov 18 02:16:38 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;  use Moose::Util::TypeConstraints;
27    
28  enum 'Runnable' => qw/data markup sponge/;  sub runnable { qw/data data.js markup sponge/ }
29    enum 'Runnable' => runnable;
 sub runnable { qw/data markup sponge/ }  
30    
31  has 'class' => (  has 'class' => (
32          is => 'rw',          is => 'rw',
# Line 41  has 'params' => ( Line 43  has 'params' => (
43  has 'run' => (  has 'run' => (
44          is => 'rw',          is => 'rw',
45          isa => 'Runnable',          isa => 'Runnable',
46            default => 'markup',
47  );  );
48    
49  sub html {  sub html {
50          my ( $self ) = @_;          my ( $self ) = @_;
51    
         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";  
   
52          my $html;          my $html;
53          my $values = {};          eval {
54          $values = $self->config($class);                  my $class = $self->class;
55          warn "# $class config = ",dump( $values );                  $self->load_class( $class );
   
         if ( @required ) {  
                 $html = qq|<h1>$class params</h1><form method="post">|;  
   
                 my $a;  
                 my @attrs = map {  $a->{$_}++; $_ } $self->attribute_order;  
                 push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @required;  
                 warn "# attrs = ",dump( @attrs );  
   
                 foreach my $name ( @attrs ) {  
                         my $attr = $class->meta->get_attribute( $name );  
                         my $type = $name =~ m/^pass/ ? 'password' : 'text';  
                         my $value = '';  
                         my $value_html = '';  
                         if ( ref($values) eq 'HASH' ) {  
                                 $value = $values->{$name};  
                         } elsif ( ref($values) eq 'ARRAY' ) {  
                                 $value_html = qq|<select name="$name">| . join("\n",  
                                         map {  
                                                 my $v = $_->{$name};  
                                                 qq|<option value="$v">$v</option>|  
                                         } @$values  
                                 ) . qq|</select>|;  
                         } elsif ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) {  
                                 $value_html = qq|<select name="$name">| . join("\n",  
                                         map { qq|<option value="$_">$_</option>| } @{ $attr->type_constraint->values }  
                                 ) . qq|</select>|;  
                         } elsif ( $attr->has_default ) {  
                                 $value = $attr->default( $name );  
                         }  
                         $value_html = qq|<input type="$type" name="$name" value="$value">| unless $value_html;  
56    
57  #warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 );                  if ( $html = $self->params_form ) {
58                          $html .= qq|<label for="$name">$name</label>| . $value_html;                          warn "got params form for $class";
59                  }                  } else {
60                  $html .= qq|<input type="submit" value="Run $class"></form>|;                          my $o;
61          } else {                          # we don't want default status elements
62                  my $o = $class->new( %{ $self->params } );                          $self->params->{status} = [];
63                  $o->depends if $o->can('depends');                          $o = $class->new( %{ $self->params } );
64                  if ( $o->can('markup') ) {                          $o->depends if $o->can('depends');
65                          warn "## using ",ref($o), "->markup";  
66                          $html = eval { $o->markup };                          if ( $self->run eq 'markup' ) {
67                          if ( $@ ) {                                  warn "## using ",ref($o), "->markup";
68                                  warn $@;                                  $html = $o->markup;
69                                  $html .= qq{<code>$@</code>};                                  # preserve status
70                                    push @{ $self->status }, { $class => $o->status } if $o->can('status');
71    
72                                    warn ">>> markup $class ",length( $html ), " bytes\n";
73                            } elsif ( $self->run eq 'sponge' ) {
74                                    my $data = $o->sponge;
75                                    confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
76                                    my $rows = $#{ $data->{rows} } + 1;
77                                    $rows ||= 'no';
78                                    $html .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>";
79                                    $html .= '<table>';
80                                    $html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
81                                    $html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
82                                    $html .= '</table>';
83                            } elsif ( $self->run =~ m{^data(\.(js|json|yaml|yml))?$} ) {
84                                    my $data = $o->data;
85                                    if ( my $format = $1 ) {
86                                            $html .= to_json( $data ) if $format =~ m{js(on)?};
87                                            $html .= Dump( $data )    if $format =~ m{yaml?};
88                                    }
89                                    if ( ! $html ) {
90                                            $html .= Frey::Dumper->new( data => $data )->markup;
91                                            push @{ $self->status }, { 'Dump' => $data };
92                                    }
93                            } else {
94                                    $html = $self->error( "IGNORE: $class ", $o->dump );
95                          }                          }
96                          warn ">>> markup $class ",length( $html ), " bytes\n";                  };
97                  } elsif ( $o->can('sponge') ) {  
98                          my $data = $o->sponge;                  if ( ref($html) eq 'HASH' ) {
99                          $html .= '<table>';                          $html = $self->page( %$html );
                         $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>';  
100                  } else {                  } else {
101                          $html = "IGNORE: $class ", $o->dump;                          $html = $self->page( title => $self->class, body => $html );
102                          warn $html;                  };
103                  }          };
104          }  
105            $html = $self->error( $@ ) if $@;
106    
107          return $self->page( title => $class, body => $html );          return $html;
108  }  }
109    
110  1;  1;

Legend:
Removed from v.349  
changed lines
  Added in v.400

  ViewVC Help
Powered by ViewVC 1.1.26