/[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 591 by dpavlin, Fri Nov 28 16:39:37 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 ( my $form = $self->params_form ) {
69                            $html = $self->page( body => $form );
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 }++;
                         $o->depends if $o->can('depends');  
   
                         push @{ $self->status }, { qq|<a target="editor" href="/editor+$class+1">$class</a>| => $self->params };  
74    
75                          if ( $self->run eq 'markup' ) {  =begin remove
76                                  warn "## using ",ref($o), "->markup";                          my $o;
77                                  if ( $o->can('page') ) {                          my ( $meta, $is_role, $instance ) = $self->class_meta( $class );
78                                          #$html = $o->page;                          if ( $is_role ) {
79                                          $body = $o->markup unless $html;                                  $o = $instance;
80                                  } else {                                  if ( $o->can('add_status') ) {
81                                          $body = $o->markup;                                          $self->TODO("missing add_status in $o");
82                                            Frey::Web->meta->apply( $o );
83                                            warn "# apply Frey::Web to $class instance $o";
84                                  }                                  }
85                            } else {
86                                    $o = $self->new_frey_class( $class, $self->params );
87                            }
88    =cut
89    
90                            my $o = $self->new_frey_class( $class, $self->params );
91                            $o->depends if $o->can('depends');
92    
93                                  warn ">>> markup $class ",length( $html || $body ), " ", $html ? 'html' : 'body', " bytes";                          if ( $self->run =~ m{as_markup} ) {
94                          } elsif ( $self->run eq 'sponge' ) {                                  $html = $o->page( run => $self->run );
95                                  $data = $o->sponge;                          } elsif ( $self->run =~ m{as_sponge} ) {
96                                    $data = $o->as_sponge;
97                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
98                                  if ( $self->format eq 'html' ) {                                  if ( $self->format eq 'html' ) {
99                                          my $rows = $#{ $data->{rows} } + 1;                                          my $rows = $#{ $data->{rows} } + 1;
100                                          $rows ||= 'no';                                          $rows ||= 'no';
101                                          $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>";
102                                          $body .= '<table>';                                          $body .= '<table>';
103                                          $body .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';                                          $body .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
104                                          $body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };                                          $body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
105                                          $body .= '</table>';                                          $body .= '</table>';
106                                  }                                  }
107                          } elsif ( $self->run eq 'data' ) {                          } elsif ( $self->run =~ m{as_data} ) {
108                                  $data = $o->data;                                  $data = $o->{$self->run}();
109                                    warn "no data from ", $self->run;
110                                    $data ||= $o->as_data();
111                                    confess "no data for $class->" . $self->run . " from " . $self->html_dump( $data ) unless $data;
112                                    $self->add_status( { $self->run => $data } );
113                          } else {                          } else {
114                                  $body = $self->error( "IGNORE: $class ", $o->dump );                                  $body = $self->error( "IGNORE: $class ", $o->dump );
115                          }                          }
# Line 105  sub html { Line 117  sub html {
117                          if ( defined $data ) {                          if ( defined $data ) {
118                                  $html .= to_json( $data ) if $self->format =~ m{js(on)?};                                  $html .= to_json( $data ) if $self->format =~ m{js(on)?};
119                                  $html .= Dump( $data )    if $self->format =~ m{ya?ml};                                  $html .= Dump( $data )    if $self->format =~ m{ya?ml};
                                 push @{ $self->status }, { 'data' => $data };  
120                          }                          }
121                          if ( ! $html ) {                          if ( ! $html ) {
122                                  $body .= Frey::Dumper->new( data => $data )->markup;                                  $body  = Frey::View::Dumper->new( data => $body )->as_markup if ref $body;
123                                    $body .= Frey::View::Dumper->new( data => $data )->as_markup if defined $data;
124                          }                          }
125    
126                          # override our status with one from object                          $self->title( $class );
                         eval {  
                                 $self->status( $o->status );  
                         };  
                         warn "can't override status: $@" if $@;  
                 };  
   
127    
128                  if ( ref($body) eq 'HASH' ) {                          $html = $o->page( body => $body ) if $body && !$html;
129                          $html = $self->page( %$body );          
130                  } elsif ( $body && ! $html ) {                          confess "no html output for $class ", $o->dump unless defined $html;
                         $html = $self->page( title => $self->class . ' run', body => $body );  
131                  };                  };
132    
133          };          };
134    
135          $html = $self->page( title => $self->class, body => dump($html) . $self->error( $@ ) ) if $@;          $self->status_parts;
136    
137            if ( $@ ) {
138                    my $o = Frey->new;
139                    Frey::Web->meta->apply( $o );
140                    $html = $o->page( body => $self->error( $@ ) );
141            }
142    
143            warn $self->class, " produced ", length($html), " bytes";
144    
145          return $html;          return $html;
146  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26