/[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 229 by dpavlin, Sat Nov 1 13:17:45 2008 UTC revision 369 by dpavlin, Mon Nov 17 14:37:48 2008 UTC
# Line 1  Line 1 
1  package Frey::Run;  package Frey::Run;
2  use Moose;  use Moose;
3  extends 'Frey';  #extends 'Frey::ClassLoader';
4    extends 'Frey::Action';
5  with 'Frey::Web';  with 'Frey::Web';
 with 'Frey::Config';  
6  with 'Frey::Escape';  with 'Frey::Escape';
7    
8    use Data::Dump qw/dump/;
9    use Frey::Dumper;
10    
11  =head1 NAME  =head1 NAME
12    
13  Frey::Run - display required form field for Class and run it  Frey::Run - display required form field for Class and run it
# Line 12  Frey::Run - display required form field Line 15  Frey::Run - display required form field
15  =head1 DESCRIPTION  =head1 DESCRIPTION
16    
17  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
18  will try to invoke C<data>, C<html> or C<markup> method on the.  will try to invoke C<data>, and C<markup> method on the.
19    
20    =head1 SEE ALSO
21    
22    L<Frey::Action> which creates form for params
23    
24  =cut  =cut
25    
26  sub execute { qw/data markup request/ }  use Moose::Util::TypeConstraints;
27    
28    enum 'Runnable' => qw/data markup sponge/;
29    
30    sub runnable { qw/data markup sponge/ }
31    
32  has 'class' => (  has 'class' => (
33          is => 'rw',          is => 'rw',
# Line 24  has 'class' => ( Line 35  has 'class' => (
35          required => 1,          required => 1,
36  );  );
37    
38  use Data::Dump qw/dump/;  has 'params' => (
39            is => 'rw',
40  sub request {          isa => 'HashRef',
41          my ( $self, $req ) = @_;          default => sub { {} },
42    );
         my %params = $req->params;  
         my $class = $self->class;  
43    
44          my @required =  has 'run' => (
45                  grep {          is => 'rw',
46                          defined $_ && !defined( $params{$_} )          isa => 'Runnable',
47                  }          default => 'markup',
48                  map {  );
                         my $attr = $class->meta->get_attribute($_);  
                         $attr->is_required && $_  
                 } $class->meta->get_attribute_list;  
49    
50                  warn "## required = ",dump( @required );  sub html {
51                  warn "## params = ",dump( %params );          my ( $self ) = @_;
52    
53          my $html;          my $html;
54          my $values = $self->config($class);          eval {
55          $values = {} if $@;                  my $class = $self->class;
56                    $self->load_class( $class );
57    
58          if ( @required ) {                  if ( $html = $self->params_form ) {
59                  $html = qq|<h1>Required params for $class</h1><form method="post">|;                          warn "got params form for $class";
                 foreach my $name ( @required ) {  
                         my $type = $name =~ m/^pass/ ? 'password' : 'text';  
                         my $value = $values ? $values->{$name} : '';  
                         $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( %params );  
                 $o->depends if $o->can('depends');  
                 if ( $o->can('request') ) {  
                         warn "## turning over to $o->request";  
                         $o->request( $req );  
                 } elsif ( $o->can('markup') ) {  
                         warn "## using ",ref($o), "->markup";  
                         $html = $o->markup;  
                         warn ">>> markup $class ",length( $html ), " bytes\n";  
                 } elsif ( $o->can('data') ) {  
                         $html = '<code>' . $self->html_escape( dump( $o->data ) ) . '</code>';  
60                  } else {                  } else {
61                          $html = "IGNORE: $class ", $o->dump;                          my $o;
62                          warn $html;                          $o = $class->new( %{ $self->params } );
63                  }                          $o->depends if $o->can('depends');
64          }  
65                            if ( $self->run eq 'markup' ) {
66                                    warn "## using ",ref($o), "->markup";
67                                    $html = $o->markup;
68                                    warn ">>> markup $class ",length( $html ), " bytes\n";
69                            } elsif ( $self->run eq 'sponge' ) {
70                                    my $data = $o->sponge;
71                                    confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
72                                    my $rows = $#{ $data->{rows} } + 1;
73                                    $rows ||= 'no';
74                                    $html .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>";
75                                    $html .= '<table>';
76                                    $html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
77                                    $html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
78                                    $html .= '</table>';
79                            } elsif ( $self->run eq 'data' ) {
80                                    my $data = $o->data;
81                                    $html .= Frey::Dumper->new( data => $data )->markup;
82                                    $html .= '<hr/><span class="frey-popdown">dump<span><code>' . $self->html_dump( $data ) . '</code></span></span>';
83                            } else {
84                                    $html = $self->error( "IGNORE: $class ", $o->dump );
85                            }
86                    };
87    
88            };
89            $html .= $self->error( $@ ) if $@;
90    
91          $req->print( $self->page( title => $class, body => $html ) );          return $self->page( %$html ) if ref($html) eq 'HASH';
92            return $self->page( title => $self->class, body => $html );
93  }  }
94    
95  1;  1;

Legend:
Removed from v.229  
changed lines
  Added in v.369

  ViewVC Help
Powered by ViewVC 1.1.26