/[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 420 by dpavlin, Tue Nov 18 10:34:42 2008 UTC revision 421 by dpavlin, Tue Nov 18 17:12:09 2008 UTC
# Line 7  with 'Frey::Escape'; Line 7  with 'Frey::Escape';
7    
8  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
9  use Frey::Dumper;  use Frey::Dumper;
10    use JSON;
11    
12  =head1 NAME  =head1 NAME
13    
# Line 28  use Moose::Util::TypeConstraints; Line 29  use Moose::Util::TypeConstraints;
29  sub runnable { qw/data data.js markup sponge/ }  sub runnable { qw/data data.js markup sponge/ }
30  enum 'Runnable' => runnable;  enum 'Runnable' => runnable;
31    
32    sub formats_available { qw/html js json yaml yml/ }
33    enum 'Formats' => formats_available;
34    
35  has 'class' => (  has 'class' => (
36          is => 'rw',          is => 'rw',
37          isa => 'Str',          isa => 'Str',
# Line 46  has 'run' => ( Line 50  has 'run' => (
50          default => 'markup',          default => 'markup',
51  );  );
52    
53    has 'format' => (
54            is => 'rw',
55            isa => 'Formats',
56            default => 'html',
57    );
58    
59  sub html {  sub html {
60          my ( $self ) = @_;          my ( $self ) = @_;
61    
62          my $html;          my ($html,$body,$data);
63          eval {          eval {
64                  my $class = $self->class;                  my $class = $self->class;
65                  $self->load_class( $class );                  $self->load_class( $class );
66    
67                  if ( $html = $self->params_form ) {                  if ( $body = $self->params_form ) {
68                          warn "got params form for $class";                          warn "got required params form for $class ", $self->run, " format: ", $self->format;
69                  } else {                  } else {
70                          my $o;  
71                          # we don't want default status elements                          my $o = $class->new( %{ $self->params } );
                         $self->params->{status} = [];  
                         $o = $class->new( %{ $self->params } );  
72                          $o->depends if $o->can('depends');                          $o->depends if $o->can('depends');
73    
74                            push @{ $self->status }, { qq|<a target="editor" href="/editor+$class+1">$class</a>| => $self->params };
75    
76                          if ( $self->run eq 'markup' ) {                          if ( $self->run eq 'markup' ) {
77                                  warn "## using ",ref($o), "->markup";                                  warn "## using ",ref($o), "->markup";
78                                  $html = $o->markup;                                  if ( $o->can('page') ) {
79                                  # preserve status                                          $html = $o->page;
80                                  push @{ $self->status }, { $class => $o->status } if $o->can('status');                                          $body = $o->markup unless $html;
81                                    } else {
82                                            $body = $o->markup;
83                                    }
84    
85                                  warn ">>> markup $class ",length( $html ), " bytes\n";                                  warn ">>> markup $class ",length( $html || $body ), " ", $html ? 'html' : 'body', " bytes";
86                          } elsif ( $self->run eq 'sponge' ) {                          } elsif ( $self->run eq 'sponge' ) {
87                                  my $data = $o->sponge;                                  $data = $o->sponge;
88                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
89                                  my $rows = $#{ $data->{rows} } + 1;                                  if ( $self->format eq 'html' ) {
90                                  $rows ||= 'no';                                          my $rows = $#{ $data->{rows} } + 1;
91                                  $html .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>";                                          $rows ||= 'no';
92                                  $html .= '<table>';                                          $body .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>";
93                                  $html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';                                          $body .= '<table>';
94                                  $html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };                                          $body .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
95                                  $html .= '</table>';                                          $body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
96                          } elsif ( $self->run =~ m{^data(\.(js|json|yaml|yml))?$} ) {                                          $body .= '</table>';
                                 my $data = $o->data;  
                                 if ( my $format = $1 ) {  
                                         $html .= to_json( $data ) if $format =~ m{js(on)?};  
                                         $html .= Dump( $data )    if $format =~ m{yaml?};  
                                 }  
                                 if ( ! $html ) {  
                                         $html .= Frey::Dumper->new( data => $data )->markup;  
                                         push @{ $self->status }, { 'Dump' => $data };  
97                                  }                                  }
98                            } elsif ( $self->run eq 'data' ) {
99                                    $data = $o->data;
100                          } else {                          } else {
101                                  $html = $self->error( "IGNORE: $class ", $o->dump );                                  $body = $self->error( "IGNORE: $class ", $o->dump );
102                            }
103    
104                            if ( defined $data ) {
105                                    $html .= to_json( $data ) if $self->format =~ m{js(on)?};
106                                    $html .= Dump( $data )    if $self->format =~ m{ya?ml};
107                                    push @{ $self->status }, { 'data' => $data };
108                            }
109                            if ( ! $html ) {
110                                    $body .= Frey::Dumper->new( data => $data )->markup;
111                          }                          }
112    
113                            # override our status with one from object
114                            $self->status( $o->status ) if $o->can('status') && $o->status;
115                  };                  };
116    
117                  if ( ref($html) eq 'HASH' ) {  
118                          $html = $self->page( %$html );                  if ( ref($body) eq 'HASH' ) {
119                  } else {                          $html = $self->page( %$body );
120                          $html = $self->page( title => $self->class, body => $html );                  } elsif ( $body && ! $html ) {
121                            $html = $self->page( title => $self->class . ' run', body => $body );
122                  };                  };
123          };          };
124    
125          $html = $self->page( title => $self->class, body => $self->error( $@ ) ) if $@;          $html = $self->page( title => $self->class, body => dump($html) . $self->error( $@ ) ) if $@;
126    
127          return $html;          return $html;
128  }  }

Legend:
Removed from v.420  
changed lines
  Added in v.421

  ViewVC Help
Powered by ViewVC 1.1.26