/[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 365 by dpavlin, Sun Nov 16 19:57:00 2008 UTC revision 1020 by dpavlin, Mon Jan 26 14:51:11 2009 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::PPI';  extends 'Frey::Action';
5  with 'Frey::Web';  with 'Frey::Session';
 with 'Frey::Config';  
 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 18  Frey::Run - display required form field Line 20  Frey::Run - display required form field
20  This object will try to run other Moose objects from your application. It  This object will try to run other Moose objects from your application. It
21  will try to invoke C<data>, and C<markup> method on the.  will try to invoke C<data>, and C<markup> method on the.
22    
23    =head1 SEE ALSO
24    
25    L<Frey::Action> which creates form for params
26    
27  =cut  =cut
28    
29  use Moose::Util::TypeConstraints;  use Moose::Util::TypeConstraints;
30    use Frey::Class::Loader; # class_runnable_re
31    
32  enum 'Runnable' => qw/data markup sponge/;  subtype 'Runnable'
33            => as 'Str',
34            => where sub { Frey::Class::Loader::class_runnable_re };
35    
36  sub runnable { qw/data markup sponge/ }  sub formats_available { qw/html js json yaml yml/ }
37    enum 'Formats' => formats_available;
38    
39  has 'class' => (  has 'class' => (
40          is => 'rw',          is => 'rw',
# Line 41  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 $class = $self->class;          my ($html,$body,$data);
78    
79          $self->load_class( $class );          my $current_status;
80            $current_status->{$_}++ foreach $self->status;
81    
82          my @required =          eval {
83                  grep {                  my $class = $self->class;
84                          defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } )                  $self->load_class( $class );
85                  }  
86                  map {                  if ( my $form = $self->params_form ) {
87                          my $attr = $class->meta->get_attribute($_);                          $html = $self->html_page( body => $form );
88                          $attr->is_required && $attr;                          warn "got required params form for $class ", $self->run, " format: ", $self->format;
89                  } $class->meta->get_attribute_list;                  } else {
90    
91          warn "## required = ",dump( map { $_->name } @required ), " for $class";                          $self->usage->{ $class }++;
92    
93          my $html;  =begin remove
94          my $values = {};  
95          $values = $self->config($class);                          my $o;
96          warn "# $class config = ",dump( $values );                          my ( $meta, $is_role, $instance ) = $self->class_meta( $class );
97                            if ( $is_role ) {
98          if ( @required ) {                                  $o = $instance;
99                  $html = qq|<h1>$class params</h1><form method="post">|;                                  if ( $o->can('add_status') ) {
100                                            $self->TODO("missing add_status in $o");
101                  my $a;                                          Frey::Web->meta->apply( $o );
102                  my @attrs = map {  $a->{$_}++; $_ } @{ $self->attribute_order };                                          warn "# apply Frey::Web to $class instance $o";
                 push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @required;  
                 warn "# attrs = ",dump( @attrs );  
   
                 foreach my $name ( grep { ! $class->meta->get_attribute($_)->is_lazy } @attrs ) {  
                         my $type = $name =~ m/^pass/ ? 'password' : 'text';  
                         my $value = '';  
                         my $value_html = '';  
                         if ( ref($values) eq 'HASH' ) {  
                                 $value = $values->{$name};  
                         } elsif ( ref($values) eq 'ARRAY' ) {  
                                 $value_html = qq|<select name="$name">| . join("\n",  
                                         map {  
                                                 my $v = $_->{$name};  
                                                 qq|<option value="$v">$v</option>|  
                                         } @$values  
                                 ) . qq|</select>|;  
                         } elsif ( my $attr = $class->meta->get_attribute( $name ) ) {  
                                 if ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) {  
                                         $value_html = qq|<select name="$name">| . join("\n",  
                                                 map { qq|<option value="$_">$_</option>| } @{ $attr->type_constraint->values }  
                                         ) . qq|</select>|;  
                                 } elsif ( $attr->has_default ) {  
                                         $value = $attr->default( $name );  
103                                  }                                  }
104                          } else {                          } else {
105                                  warn "wired attribute $name";                                  $o = $self->new_frey_class( $class, $self->params );
106                          }                          }
107                          $value_html = qq|<input type="$type" name="$name" value="$value">| unless $value_html;  =cut
108    
109  #warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 );                          my $o = $self->new_frey_class( $class, $self->params );
                         $html .= qq|<label for="$name">$name</label>| . $value_html;  
                 }  
                 $html .= qq|<input type="submit" value="Run $class"></form>|;  
         } else {  
                 eval {  
                         my $o;  
                         $o = $class->new( %{ $self->params } );  
110                          $o->depends if $o->can('depends');                          $o->depends if $o->can('depends');
111    
112                          if ( $self->run eq 'markup' ) {                          if ( $self->run =~ m{as_markup} ) {
113                                  warn "## using ",ref($o), "->markup";                                  $html = $o->html_page( run => $self->run );
114                                  $html = $o->markup;                          } elsif ( $self->run =~ m{(.*as_sponge)} ) {
115                                  warn ">>> markup $class ",length( $html ), " bytes\n";                                  $data = $o->$1;
                         } elsif ( $self->run eq 'sponge' ) {  
                                 my $data = $o->sponge;  
116                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
117                                  my $rows = $#{ $data->{rows} } + 1;                                  if ( $self->format eq 'html' ) {
118                                  $rows ||= 'no';                                          my $rows = $#{ $data->{rows} } + 1;
119                                  $html .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>";                                          $rows ||= 'no';
120                                  $html .= '<table>';                                          $body .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->as_sponge</code>";
121                                  $html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';                                          $body .= '<table>';
122                                  $html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };                                          $body .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
123                                  $html .= '</table>';                                          $body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
124                          } elsif ( $self->run eq 'data' ) {                                          $body .= '</table>';
125                                  my $data = $o->data;  
126                                  $html .= Frey::Dumper->new( data => $data )->markup;                                          $self->add_css(qq|
127                                  $html .= '<hr/><span class="frey-popdown">dump<span><code>' . $self->html_dump( $data ) . '</code></span></span>';                                                  tr:nth-child(even) {
128                                                            background-color: #eee;
129                                                    }
130                                            |);
131    
132                                            delete( $data->{rows} ); # too much dumplication
133                                            $body .= Frey::View::Dumper->new( data => $data )->as_markup if $data;
134                                    }
135                            } elsif ( $self->run =~ m{(as_data|sql)} ) {
136                                    my $run = $self->run;
137                                    $data = $o->$run;
138                                    confess "no data for $class->$run" unless defined $data;
139                                    $self->add_status( { $self->run => $data } );
140                          } else {                          } else {
141                                  $html = $self->error( "IGNORE: $class ", $o->dump );                                  $body = $self->error( "IGNORE: $class ", $o->dump );
142                            }
143    
144                            if ( defined $data ) {
145                                    $html .= to_json( $data ) if $self->format =~ m{js(on)?};
146                                    $html .= Dump( $data )    if $self->format =~ m{ya?ml};
147                            }
148                            if ( ! $html ) {
149                                    $body  = Frey::View::Dumper->new( data => $body )->as_markup if ref $body;
150                                    $body .= Frey::View::Dumper->new( data => $data )->as_markup if defined $data && ! defined $body;
151                            }
152    
153                            $o->title( $class );
154    
155                            $html = $o->html_page( body => $body ) if $body && !$html;
156                            $self->content_type( $o->content_type );
157    
158                            confess "no html output for $class ", $o->dump unless defined $html;
159    
160                            if ( $o->can('status') ) {
161                                    foreach ( $o->status ) {
162                                            next if $current_status->{$_}++;
163                                            $self->add_status( $_ );
164                                            warn "# run add_status: ", $self->dump( $_ );
165                                    }
166                          }                          }
167    
168                  };                  };
169    
170                  $html .= $self->error( $@ ) if $@;          };
171    
172            $self->status_parts;
173    
174            if ( $@ ) {
175                    my $error = $@;
176                    warn $error;
177    
178                    exit if $error =~ m{Attempt to reload \S+ aborted}; # FIXME Mojo can't reload DBIx::Class
179    
180                    my $o = Frey->new;
181                    $o->{request_url} = $self->request_url; # sigh, this is dynamic languge, right?
182                    Frey::Web->meta->apply( $o );
183                    $html = $o->html_page( body => $self->error( $error, undef ) );
184          }          }
185    
186          return $self->page( %$html ) if ref($html) eq 'HASH';          warn $self->class, " produced ", length($html), " bytes of ", $self->content_type;
187          return $self->page( title => $class, body => $html );  
188            return $html;
189  }  }
190    
191  1;  1;

Legend:
Removed from v.365  
changed lines
  Added in v.1020

  ViewVC Help
Powered by ViewVC 1.1.26