/[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 348 by dpavlin, Sat Nov 15 23:52:22 2008 UTC revision 887 by dpavlin, Wed Dec 24 13:51:59 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::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  sub runnable { qw/data markup sponge/ }  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 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 34  has 'params' => ( Line 48  has 'params' => (
48          default => sub { {} },          default => sub { {} },
49  );  );
50    
51    has 'run' => (
52            is => 'rw',
53            isa => 'Runnable',
54            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            eval {
79                    my $class = $self->class;
80                    $self->load_class( $class );
81    
82                    if ( my $form = $self->params_form ) {
83                            $html = $self->page( body => $form );
84                            warn "got required params form for $class ", $self->run, " format: ", $self->format;
85                    } else {
86    
87                            $self->usage->{ $class }++;
88    
89    =begin remove
90    
91                            my $o;
92                            my ( $meta, $is_role, $instance ) = $self->class_meta( $class );
93                            if ( $is_role ) {
94                                    $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          $self->load_class( $class );                          my $o = $self->new_frey_class( $class, $self->params );
106                            $o->depends if $o->can('depends');
107    
108          my @required =                          if ( $self->run =~ m{as_markup} ) {
109                  grep {                                  $html = $o->page( run => $self->run );
110                          defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } )                          } elsif ( $self->run =~ m{(.*as_sponge)} ) {
111                  }                                  $data = $o->$1;
112                  map {                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
113                          my $attr = $class->meta->get_attribute($_);                                  if ( $self->format eq 'html' ) {
114                          $attr->is_required && $attr;                                          my $rows = $#{ $data->{rows} } + 1;
115                  } $class->meta->get_attribute_list;                                          $rows ||= 'no';
116                                            $body .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->as_sponge</code>";
117          warn "## required = ",dump( map { $_->name } @required ), " for $class";                                          $body .= '<table>';
118                                            $body .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
119          my $html;                                          $body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
120          my $values = {};                                          $body .= '</table>';
121          $values = $self->config($class);                                  }
122          warn "# $class config = ",dump( $values );                          } elsif ( $self->run =~ m{(as_data|sql)} ) {
123                                    my $run = $self->run;
124          if ( @required ) {                                  $data = $o->$run;
125                  $html = qq|<h1>$class params</h1><form method="post">|;                                  confess "no data for $class->$run" unless defined $data;
126                                    $self->add_status( { $self->run => $data } );
                 my $a;  
                 my @attrs = map {  $a->{$_}++; $_ } $self->attribute_order;  
                 push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @required;  
                 warn "# attrs = ",dump( @attrs );  
   
                 foreach my $name ( @attrs ) {  
                         my $attr = $class->meta->get_attribute( $name );  
                         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>|;  
127                          } else {                          } else {
128                                  $value = $attr->default( $name ) if $attr->has_default;                                  $body = $self->error( "IGNORE: $class ", $o->dump );
129                          }                          }
                         $value_html = qq|<input type="$type" name="$name" value="$value">| unless $value_html;  
130    
131  #warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 );                          if ( defined $data ) {
132                          $html .= qq|<label for="$name">$name</label>| . $value_html;                                  $html .= to_json( $data ) if $self->format =~ m{js(on)?};
133                  }                                  $html .= Dump( $data )    if $self->format =~ m{ya?ml};
                 $html .= qq|<input type="submit" value="Run $class"></form>|;  
         } else {  
                 my $o = $class->new( %{ $self->params } );  
                 $o->depends if $o->can('depends');  
                 if ( $o->can('markup') ) {  
                         warn "## using ",ref($o), "->markup";  
                         $html = eval { $o->markup };  
                         if ( $@ ) {  
                                 warn $@;  
                                 $html .= qq{<code>$@</code>};  
134                          }                          }
135                          warn ">>> markup $class ",length( $html ), " bytes\n";                          if ( ! $html ) {
136                  } elsif ( $o->can('sponge') ) {                                  $body  = Frey::View::Dumper->new( data => $body )->as_markup if ref $body;
137                          my $data = $o->sponge;                                  $body .= Frey::View::Dumper->new( data => $data )->as_markup if defined $data;
138                          $html .= '<table>';                          }
139                          $html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';  
140                          $html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };                          $o->title( $class );
141                          $html .= '</table>';  
142                  } elsif ( $o->can('data') ) {                          $html = $o->page( body => $body ) if $body && !$html;
143                          my $data = $o->data;                          $self->content_type( $o->content_type );
144                          $html .= Frey::Dumper->new( data => $data )->markup;  
145                          $html .= '<hr/><code>' . $self->html_dump( $data ) . '</code>';                          confess "no html output for $class ", $o->dump unless defined $html;
146                  } else {                  };
147                          $html = "IGNORE: $class ", $o->dump;  
148                          warn $html;          };
149                  }  
150            $self->status_parts;
151    
152            if ( $@ ) {
153                    my $error = $@;
154                    my $o = Frey->new;
155                    $o->{request_url} = $self->request_url; # sigh, this is dynamic languge, right?
156                    Frey::Web->meta->apply( $o );
157                    $html = $o->page( body => $self->error( $error, undef ) );
158          }          }
159    
160          return $self->page( title => $class, body => $html );          warn $self->class, " produced ", length($html), " bytes of ", $self->content_type;
161    
162            return $html;
163  }  }
164    
165  1;  1;

Legend:
Removed from v.348  
changed lines
  Added in v.887

  ViewVC Help
Powered by ViewVC 1.1.26