/[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 317 by dpavlin, Wed Nov 5 22:05:02 2008 UTC revision 934 by dpavlin, Tue Jan 6 00:22:56 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  with 'Frey::Web';  extends 'Frey::Action';
5  with 'Frey::Config';  with 'Frey::Session';
6  with 'Frey::Escape';  
7    use Data::Dump qw/dump/;
8    use JSON;
9    use YAML;
10    
11    use lib 'lib';
12    use Frey::View::Dumper;
13    
14  =head1 NAME  =head1 NAME
15    
# Line 14  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/ }  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 30  has 'params' => ( Line 48  has 'params' => (
48          default => sub { {} },          default => sub { {} },
49  );  );
50    
51  use Data::Dump qw/dump/;  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    
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->page( body => $form );
88                          $attr->is_required && $attr;                          warn "got required params form for $class ", $self->run, " format: ", $self->format;
                 } $class->meta->get_attribute_list;  
   
         warn "## required = ",dump( map { $_->name } @required ), " for $class";  
   
         my $html;  
         my $values = {};  
         $values = $self->config($class) if $self->can('config');  
   
         if ( @required ) {  
                 $html = qq|<h1>Required params for $class</h1><form method="post">|;  
                 foreach my $attr ( @required ) {  
                         my $name = $attr->name;  
                         my $type = $name =~ m/^pass/ ? 'password' : 'text';  
                         my $value =  
                                 $values ? $values->{$name} :  
                                 $attr->has_default ? $attr->default( $name ) :  
                                 '';  
 #warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 );  
                         $html .= qq|<label for="$name">$name</label><input type="$type" name="$name" value="$value">|;  
                 }  
                 $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>};  
                         }  
                         warn ">>> markup $class ",length( $html ), " bytes\n";  
                 } elsif ( $o->can('data') ) {  
                         $html = '<code>' . $self->html_escape( dump( $o->data ) ) . '</code>';  
89                  } else {                  } else {
90                          $html = "IGNORE: $class ", $o->dump;  
91                          warn $html;                          $self->usage->{ $class }++;
92                  }  
93    =begin remove
94    
95                            my $o;
96                            my ( $meta, $is_role, $instance ) = $self->class_meta( $class );
97                            if ( $is_role ) {
98                                    $o = $instance;
99                                    if ( $o->can('add_status') ) {
100                                            $self->TODO("missing add_status in $o");
101                                            Frey::Web->meta->apply( $o );
102                                            warn "# apply Frey::Web to $class instance $o";
103                                    }
104                            } else {
105                                    $o = $self->new_frey_class( $class, $self->params );
106                            }
107    =cut
108    
109                            my $o = $self->new_frey_class( $class, $self->params );
110                            $o->depends if $o->can('depends');
111    
112                            if ( $self->run =~ m{as_markup} ) {
113                                    $html = $o->page( run => $self->run );
114                            } elsif ( $self->run =~ m{(.*as_sponge)} ) {
115                                    $data = $o->$1;
116                                    confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
117                                    if ( $self->format eq 'html' ) {
118                                            my $rows = $#{ $data->{rows} } + 1;
119                                            $rows ||= 'no';
120                                            $body .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->as_sponge</code>";
121                                            $body .= '<table>';
122                                            $body .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
123                                            $body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
124                                            $body .= '</table>';
125                                            
126                                            delete( $data->{rows} ); # too much dumplication
127                                            $body .= Frey::View::Dumper->new( data => $data )->as_markup if $data;
128                                    }
129                            } elsif ( $self->run =~ m{(as_data|sql)} ) {
130                                    my $run = $self->run;
131                                    $data = $o->$run;
132                                    confess "no data for $class->$run" unless defined $data;
133                                    $self->add_status( { $self->run => $data } );
134                            } else {
135                                    $body = $self->error( "IGNORE: $class ", $o->dump );
136                            }
137    
138                            if ( defined $data ) {
139                                    $html .= to_json( $data ) if $self->format =~ m{js(on)?};
140                                    $html .= Dump( $data )    if $self->format =~ m{ya?ml};
141                            }
142                            if ( ! $html ) {
143                                    $body  = Frey::View::Dumper->new( data => $body )->as_markup if ref $body;
144                                    $body .= Frey::View::Dumper->new( data => $data )->as_markup if defined $data && ! defined $body;
145                            }
146    
147                            $o->title( $class );
148    
149                            $html = $o->page( body => $body ) if $body && !$html;
150                            $self->content_type( $o->content_type );
151    
152                            confess "no html output for $class ", $o->dump unless defined $html;
153    
154                            if ( $o->can('status') ) {
155                                    foreach ( $o->status ) {
156                                            next if $current_status->{$_}++;
157                                            $self->add_status( $_ );
158                                            warn "# run add_status: ", $self->dump( $_ );
159                                    }
160                            }
161    
162                    };
163    
164            };
165    
166            $self->status_parts;
167    
168            if ( $@ ) {
169                    my $error = $@;
170                    my $o = Frey->new;
171                    $o->{request_url} = $self->request_url; # sigh, this is dynamic languge, right?
172                    Frey::Web->meta->apply( $o );
173                    $html = $o->page( body => $self->error( $error, undef ) );
174          }          }
175    
176          return $self->page( title => $class, body => $html );          warn $self->class, " produced ", length($html), " bytes of ", $self->content_type;
177    
178            return $html;
179  }  }
180    
181  1;  1;

Legend:
Removed from v.317  
changed lines
  Added in v.934

  ViewVC Help
Powered by ViewVC 1.1.26