/[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 423 by dpavlin, Tue Nov 18 19:50:45 2008 UTC revision 581 by dpavlin, Fri Nov 28 13:16:47 2008 UTC
# Line 2  package Frey::Run; Line 2  package Frey::Run;
2  use Moose;  use Moose;
3  #extends 'Frey::ClassLoader';  #extends 'Frey::ClassLoader';
4  extends 'Frey::Action';  extends 'Frey::Action';
5  with 'Frey::Web';  with 'Frey::Session';
 with 'Frey::Escape';  
6    
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8  use Frey::Dumper;  use Frey::View::Dumper;
9  use JSON;  use JSON;
10  use YAML;  use YAML;
11    
# Line 27  L<Frey::Action> which creates form for p Line 26  L<Frey::Action> which creates form for p
26    
27  use Moose::Util::TypeConstraints;  use Moose::Util::TypeConstraints;
28    
29  sub runnable { qw/data data.js markup sponge/ }  subtype 'Runnable'
30  enum 'Runnable' => runnable;          => as 'Str',
31            => where sub { m{^as_} || m{_as_} };
32    
33  sub formats_available { qw/html js json yaml yml/ }  sub formats_available { qw/html js json yaml yml/ }
34  enum 'Formats' => formats_available;  enum 'Formats' => formats_available;
# Line 48  has 'params' => ( Line 48  has 'params' => (
48  has 'run' => (  has 'run' => (
49          is => 'rw',          is => 'rw',
50          isa => 'Runnable',          isa => 'Runnable',
51          default => 'markup',          default => 'as_markup',
52  );  );
53    
54  has 'format' => (  has 'format' => (
# Line 65  sub html { Line 65  sub html {
65                  my $class = $self->class;                  my $class = $self->class;
66                  $self->load_class( $class );                  $self->load_class( $class );
67    
68                  if ( $body = $self->params_form ) {                  if ( $self->params_form ) {
69                            $html = $self->page;
70                          warn "got required params form for $class ", $self->run, " format: ", $self->format;                          warn "got required params form for $class ", $self->run, " format: ", $self->format;
71                  } else {                  } else {
72    
73                          my $o = $class->new( %{ $self->params } );                          $self->usage->{ $class }++;
74                          $o->depends if $o->can('depends');  
75                            my $o;
76                            my ( $meta, $is_role, $instance ) = $self->class_meta( $class );
77                            if ( $is_role ) {
78                                    $o = $instance;
79                            } else {
80                                    $o = $self->new_frey_class( $class, $self->params );
81                            }
82    
83                          push @{ $self->status }, { qq|<a target="editor" href="/editor+$class+1">$class</a>| => $self->params };                          $o->depends if $o->can('depends');
84    
85                          if ( $self->run eq 'markup' ) {                          $o->add_status( { $self->editor( $class ) => $self->params } );
                                 warn "## using ",ref($o), "->markup";  
                                 if ( $o->can('page') ) {  
                                         #$html = $o->page;  
                                         $body = $o->markup unless $html;  
                                 } else {  
                                         $body = $o->markup;  
                                 }  
86    
87                                  warn ">>> markup $class ",length( $html || $body ), " ", $html ? 'html' : 'body', " bytes";                          if ( $self->run =~ m{as_markup} ) {
88                          } elsif ( $self->run eq 'sponge' ) {                                  $html = $o->page;
89                                  $data = $o->sponge;                          } elsif ( $self->run =~ m{as_sponge} ) {
90                                    $data = $o->as_sponge;
91                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
92                                  if ( $self->format eq 'html' ) {                                  if ( $self->format eq 'html' ) {
93                                          my $rows = $#{ $data->{rows} } + 1;                                          my $rows = $#{ $data->{rows} } + 1;
94                                          $rows ||= 'no';                                          $rows ||= 'no';
95                                          $body .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>";                                          $body .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->as_sponge</code>";
96                                          $body .= '<table>';                                          $body .= '<table>';
97                                          $body .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';                                          $body .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
98                                          $body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };                                          $body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
99                                          $body .= '</table>';                                          $body .= '</table>';
100                                  }                                  }
101                          } elsif ( $self->run eq 'data' ) {                          } elsif ( $self->run =~ m{as_data} ) {
102                                  $data = $o->data;                                  $data = $o->as_data;
103                          } else {                          } else {
104                                  $body = $self->error( "IGNORE: $class ", $o->dump );                                  $body = $self->error( "IGNORE: $class ", $o->dump );
105                          }                          }
# Line 105  sub html { Line 107  sub html {
107                          if ( defined $data ) {                          if ( defined $data ) {
108                                  $html .= to_json( $data ) if $self->format =~ m{js(on)?};                                  $html .= to_json( $data ) if $self->format =~ m{js(on)?};
109                                  $html .= Dump( $data )    if $self->format =~ m{ya?ml};                                  $html .= Dump( $data )    if $self->format =~ m{ya?ml};
110                                  push @{ $self->status }, { 'data' => $data };                                  $self->add_status( { 'data' => $data } );
111                          }                          }
112                          if ( ! $html ) {                          if ( ! $html ) {
113                                  $body .= Frey::Dumper->new( data => $data )->markup;                                  $body  = Frey::View::Dumper->new( data => $body )->as_markup if ref $body;
114                                    $body .= Frey::View::Dumper->new( data => $data )->as_markup if defined $data;
115                          }                          }
116    
117                          # override our status with one from object                          $self->title( $class );
                         eval {  
                                 $self->status( $o->status );  
                         };  
                         warn "can't override status: $@" if $@;  
                 };  
   
118    
119                  if ( ref($body) eq 'HASH' ) {                          $html = $o->page( body => $body ) if $body && !$html;
                         $html = $self->page( %$body );  
                 } elsif ( $body && ! $html ) {  
                         $html = $self->page( title => $self->class . ' run', body => $body );  
120                  };                  };
121    
122          };          };
123    
124          $html = $self->page( title => $self->class, body => dump($html) . $self->error( $@ ) ) if $@;          $self->status_parts;
125    
126            $html = $self->error( $@ ) if $@;
127    
128            warn $self->class, " produced ", length($html), " bytes";
129    
130          return $html;          return $html;
131  }  }

Legend:
Removed from v.423  
changed lines
  Added in v.581

  ViewVC Help
Powered by ViewVC 1.1.26