/[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 400 by dpavlin, Tue Nov 18 02:16:38 2008 UTC revision 898 by dpavlin, Wed Dec 24 22:20:10 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';
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 JSON;
9    use YAML;
10    
11    use lib 'lib';
12    use Frey::View::Dumper;
13    
14  =head1 NAME  =head1 NAME
15    
# Line 24  L<Frey::Action> which creates form for p Line 27  L<Frey::Action> which creates form for p
27  =cut  =cut
28    
29  use Moose::Util::TypeConstraints;  use Moose::Util::TypeConstraints;
30    use Frey::Class::Loader; # class_runnable_re
31    
32    subtype 'Runnable'
33            => as 'Str',
34            => where sub { Frey::Class::Loader::class_runnable_re };
35    
36  sub runnable { qw/data data.js markup sponge/ }  sub formats_available { qw/html js json yaml yml/ }
37  enum 'Runnable' => runnable;  enum 'Formats' => formats_available;
38    
39  has 'class' => (  has 'class' => (
40          is => 'rw',          is => 'rw',
# Line 43  has 'params' => ( Line 51  has 'params' => (
51  has 'run' => (  has 'run' => (
52          is => 'rw',          is => 'rw',
53          isa => 'Runnable',          isa => 'Runnable',
54          default => 'markup',          default => 'as_markup',
55    );
56    
57    has 'format' => (
58            is => 'rw',
59            isa => 'Formats',
60            default => 'html',
61    );
62    
63    has 'request_url' => (
64            documentation => 'Take url from params if not specified',
65            is => 'rw',
66            isa => 'Uri', coerce => 1,
67            lazy => 1,
68            default => sub {
69                    my $self = shift;
70                    $self->params->{request_url};
71            },
72  );  );
73    
74  sub html {  sub html {
75          my ( $self ) = @_;          my ( $self ) = @_;
76    
77          my $html;          my ($html,$body,$data);
78          eval {          eval {
79                  my $class = $self->class;                  my $class = $self->class;
80                  $self->load_class( $class );                  $self->load_class( $class );
81    
82                  if ( $html = $self->params_form ) {                  if ( my $form = $self->params_form ) {
83                          warn "got params form for $class";                          $html = $self->page( body => $form );
84                            warn "got required params form for $class ", $self->run, " format: ", $self->format;
85                  } else {                  } else {
86    
87                            $self->usage->{ $class }++;
88    
89    =begin remove
90    
91                          my $o;                          my $o;
92                          # we don't want default status elements                          my ( $meta, $is_role, $instance ) = $self->class_meta( $class );
93                          $self->params->{status} = [];                          if ( $is_role ) {
94                          $o = $class->new( %{ $self->params } );                                  $o = $instance;
95                                    if ( $o->can('add_status') ) {
96                                            $self->TODO("missing add_status in $o");
97                                            Frey::Web->meta->apply( $o );
98                                            warn "# apply Frey::Web to $class instance $o";
99                                    }
100                            } else {
101                                    $o = $self->new_frey_class( $class, $self->params );
102                            }
103    =cut
104    
105                            my $o = $self->new_frey_class( $class, $self->params );
106                          $o->depends if $o->can('depends');                          $o->depends if $o->can('depends');
107    
108                          if ( $self->run eq 'markup' ) {                          if ( $self->run =~ m{as_markup} ) {
109                                  warn "## using ",ref($o), "->markup";                                  $html = $o->page( run => $self->run );
110                                  $html = $o->markup;                          } elsif ( $self->run =~ m{(.*as_sponge)} ) {
111                                  # preserve status                                  $data = $o->$1;
                                 push @{ $self->status }, { $class => $o->status } if $o->can('status');  
   
                                 warn ">>> markup $class ",length( $html ), " bytes\n";  
                         } elsif ( $self->run eq 'sponge' ) {  
                                 my $data = $o->sponge;  
112                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
113                                  my $rows = $#{ $data->{rows} } + 1;                                  if ( $self->format eq 'html' ) {
114                                  $rows ||= 'no';                                          my $rows = $#{ $data->{rows} } + 1;
115                                  $html .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>";                                          $rows ||= 'no';
116                                  $html .= '<table>';                                          $body .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->as_sponge</code>";
117                                  $html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';                                          $body .= '<table>';
118                                  $html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };                                          $body .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
119                                  $html .= '</table>';                                          $body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
120                          } elsif ( $self->run =~ m{^data(\.(js|json|yaml|yml))?$} ) {                                          $body .= '</table>';
121                                  my $data = $o->data;                                          
122                                  if ( my $format = $1 ) {                                          delete( $data->{rows} ); # too much dumplication
123                                          $html .= to_json( $data ) if $format =~ m{js(on)?};                                          $body .= Frey::View::Dumper->new( data => $data )->as_markup if $data;
                                         $html .= Dump( $data )    if $format =~ m{yaml?};  
                                 }  
                                 if ( ! $html ) {  
                                         $html .= Frey::Dumper->new( data => $data )->markup;  
                                         push @{ $self->status }, { 'Dump' => $data };  
124                                  }                                  }
125                            } elsif ( $self->run =~ m{(as_data|sql)} ) {
126                                    my $run = $self->run;
127                                    $data = $o->$run;
128                                    confess "no data for $class->$run" unless defined $data;
129                                    $self->add_status( { $self->run => $data } );
130                          } else {                          } else {
131                                  $html = $self->error( "IGNORE: $class ", $o->dump );                                  $body = $self->error( "IGNORE: $class ", $o->dump );
132                          }                          }
                 };  
133    
134                  if ( ref($html) eq 'HASH' ) {                          if ( defined $data ) {
135                          $html = $self->page( %$html );                                  $html .= to_json( $data ) if $self->format =~ m{js(on)?};
136                  } else {                                  $html .= Dump( $data )    if $self->format =~ m{ya?ml};
137                          $html = $self->page( title => $self->class, body => $html );                          }
138                            if ( ! $html ) {
139                                    $body  = Frey::View::Dumper->new( data => $body )->as_markup if ref $body;
140                                    $body .= Frey::View::Dumper->new( data => $data )->as_markup if defined $data && ! defined $body;
141                            }
142    
143                            $o->title( $class );
144    
145                            $html = $o->page( body => $body ) if $body && !$html;
146                            $self->content_type( $o->content_type );
147    
148                            confess "no html output for $class ", $o->dump unless defined $html;
149                  };                  };
150    
151          };          };
152    
153          $html = $self->error( $@ ) if $@;          $self->status_parts;
154    
155            if ( $@ ) {
156                    my $error = $@;
157                    my $o = Frey->new;
158                    $o->{request_url} = $self->request_url; # sigh, this is dynamic languge, right?
159                    Frey::Web->meta->apply( $o );
160                    $html = $o->page( body => $self->error( $error, undef ) );
161            }
162    
163            warn $self->class, " produced ", length($html), " bytes of ", $self->content_type;
164    
165          return $html;          return $html;
166  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26