/[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 535 by dpavlin, Wed Nov 26 16:17:17 2008 UTC revision 797 by dpavlin, Wed Dec 10 19:34:06 2008 UTC
# Line 1  Line 1 
1  package Frey::Run;  package Frey::Run;
2  use Moose;  use Moose;
3  #extends 'Frey::ClassLoader';  #extends 'Frey::Class::Loader';
4  extends 'Frey::Action';  extends 'Frey::Action';
 with 'Frey::Web';  
5  with 'Frey::Session';  with 'Frey::Session';
6    
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
# Line 29  use Moose::Util::TypeConstraints; Line 28  use Moose::Util::TypeConstraints;
28    
29  subtype 'Runnable'  subtype 'Runnable'
30          => as 'Str',          => as 'Str',
31          => where sub { m{^as_} };          => 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 58  has 'format' => ( Line 57  has 'format' => (
57          default => 'html',          default => 'html',
58  );  );
59    
60    has 'request_url' => (
61            documentation => 'Take url from params if not specified',
62            is => 'rw',
63            isa => 'Uri', coerce => 1,
64            lazy => 1,
65            default => sub {
66                    my $self = shift;
67                    $self->params->{request_url};
68            },
69    );
70    
71  sub html {  sub html {
72          my ( $self ) = @_;          my ( $self ) = @_;
73    
# Line 66  sub html { Line 76  sub html {
76                  my $class = $self->class;                  my $class = $self->class;
77                  $self->load_class( $class );                  $self->load_class( $class );
78    
79                  if ( $body = $self->params_form ) {                  if ( my $form = $self->params_form ) {
80                            $html = $self->page( body => $form );
81                          warn "got required params form for $class ", $self->run, " format: ", $self->format;                          warn "got required params form for $class ", $self->run, " format: ", $self->format;
82                  } else {                  } else {
83    
84                          $self->usage->{ $class }++;                          $self->usage->{ $class }++;
85    
86    =begin remove
87    
88                          my $o;                          my $o;
89                          my ( $meta, $is_role, $instance ) = $self->class_meta( $class );                          my ( $meta, $is_role, $instance ) = $self->class_meta( $class );
90                          if ( $is_role ) {                          if ( $is_role ) {
91                                  $o = $instance;                                  $o = $instance;
92                                    if ( $o->can('add_status') ) {
93                                            $self->TODO("missing add_status in $o");
94                                            Frey::Web->meta->apply( $o );
95                                            warn "# apply Frey::Web to $class instance $o";
96                                    }
97                          } else {                          } else {
98                                  $o = $self->new_frey_class( $class, $self->params );                                  $o = $self->new_frey_class( $class, $self->params );
99                          }                          }
100    =cut
101    
102                            my $o = $self->new_frey_class( $class, $self->params );
103                          $o->depends if $o->can('depends');                          $o->depends if $o->can('depends');
104    
105                          my @status;                          if ( $self->run =~ m{as_markup} ) {
106                                    $html = $o->page( run => $self->run );
107                          push @status, { $self->editor( $class ) => $self->params };                          } elsif ( $self->run =~ m{as_sponge} ) {
   
                         if ( $self->run eq 'as_markup' && ! $o->can('page') ) {  
                                 warn "## using ",ref($o), "->as_markup";  
                                 $body = $o->as_markup unless $html;  
                                 warn ">>> markup $class ",length( $body ), " ", $html ? 'html' : 'body', " bytes";  
                         } elsif ( $self->run eq 'as_sponge' ) {  
108                                  $data = $o->as_sponge;                                  $data = $o->as_sponge;
109                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
110                                  if ( $self->format eq 'html' ) {                                  if ( $self->format eq 'html' ) {
# Line 102  sub html { Line 116  sub html {
116                                          $body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };                                          $body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
117                                          $body .= '</table>';                                          $body .= '</table>';
118                                  }                                  }
119                          } elsif ( $self->run eq 'as_data' ) {                          } elsif ( $self->run =~ m{as_data} ) {
120                                  $data = $o->as_data;                                  my $run = $self->run;
121                                    $data = $o->$run;
122                                    confess "no data for $class->$run" unless defined $data;
123                                    $self->add_status( { $self->run => $data } );
124                          } else {                          } else {
125                                  $body = $self->error( "IGNORE: $class ", $o->dump );                                  $body = $self->error( "IGNORE: $class ", $o->dump );
126                          }                          }
# Line 111  sub html { Line 128  sub html {
128                          if ( defined $data ) {                          if ( defined $data ) {
129                                  $html .= to_json( $data ) if $self->format =~ m{js(on)?};                                  $html .= to_json( $data ) if $self->format =~ m{js(on)?};
130                                  $html .= Dump( $data )    if $self->format =~ m{ya?ml};                                  $html .= Dump( $data )    if $self->format =~ m{ya?ml};
                                 push @status, { 'data' => $data };  
131                          }                          }
132                          if ( ! $html ) {                          if ( ! $html ) {
133                                  $body  = Frey::View::Dumper->new( data => $body )->as_markup if ref $body;                                  $body  = Frey::View::Dumper->new( data => $body )->as_markup if ref $body;
134                                  $body .= Frey::View::Dumper->new( data => $data )->as_markup if defined $data;                                  $body .= Frey::View::Dumper->new( data => $data )->as_markup if defined $data;
135                          }                          }
136    
137                          warn "## status from $self ",dump(@status);                          $o->title( $class );
138    
139                          if ( $o->can('add_status') ) {                          $html = $o->page( body => $body ) if $body && !$html;
140                                  $o->add_status($_) foreach @status;                          $self->content_type( $o->content_type );
                         }  
141    
142                          if ( $self->run eq 'as_markup' && $o->can('page') ) {                          confess "no html output for $class ", $o->dump unless defined $html;
                                 $html = $o->page;  
                                 warn "got ", length($html), " for page from $o it's status is ", dump( $o->status );  
                         } else {  
                                 $self->add_status($_) foreach @status;  
                         }  
143                  };                  };
144    
                 if ( ref($body) eq 'HASH' ) {  
                         $html = $self->page( %$body );  
                         warn "WARNING: old calling convention with HASH which is depriciated but produced ", length($html), " bytes";  
                 } elsif ( $body && ! $html ) {  
                         $html = $self->page( title => $self->class . ' run', body => $body );  
                         warn "wrap body of ",length($body), " in page with ", length($html), " bytes";  
                 };  
145          };          };
146    
147          $self->status_parts;          $self->status_parts;
148    
149          $html = $self->page( title => $self->class, body => $self->error( $@ ) ) if $@;          if ( $@ ) {
150                    my $error = $@;
151                    my $o = Frey->new;
152                    $o->{request_url} = $self->request_url; # sigh, this is dynamic languge, right?
153                    Frey::Web->meta->apply( $o );
154                    $html = $o->page( body => $self->error( $error ) );
155            }
156    
157            warn $self->class, " produced ", length($html), " bytes of ", $self->content_type;
158    
159          return $html;          return $html;
160  }  }

Legend:
Removed from v.535  
changed lines
  Added in v.797

  ViewVC Help
Powered by ViewVC 1.1.26