/[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 327 by dpavlin, Thu Nov 6 20:56:44 2008 UTC revision 400 by dpavlin, Tue Nov 18 02:16:38 2008 UTC
# Line 1  Line 1 
1  package Frey::Run;  package Frey::Run;
2  use Moose;  use Moose;
3  extends 'Frey::ClassLoader';  #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/;  use Data::Dump qw/dump/;
# Line 17  Frey::Run - display required form field Line 17  Frey::Run - display required form field
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>, and 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 runnable { qw/data markup/ }  use Moose::Util::TypeConstraints;
27    
28    sub runnable { qw/data data.js markup sponge/ }
29    enum 'Runnable' => runnable;
30    
31  has 'class' => (  has 'class' => (
32          is => 'rw',          is => 'rw',
# Line 33  has 'params' => ( Line 40  has 'params' => (
40          default => sub { {} },          default => sub { {} },
41  );  );
42    
43    has 'run' => (
44            is => 'rw',
45            isa => 'Runnable',
46            default => 'markup',
47    );
48    
49  sub html {  sub html {
50          my ( $self ) = @_;          my ( $self ) = @_;
51    
         my $class = $self->class;  
   
         $self->load_class( $class );  
   
         my @required =  
                 grep {  
                         defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } )  
                 }  
                 map {  
                         my $attr = $class->meta->get_attribute($_);  
                         $attr->is_required && $attr;  
                 } $class->meta->get_attribute_list;  
   
         warn "## required = ",dump( map { $_->name } @required ), " for $class";  
   
52          my $html;          my $html;
53          my $values = {};          eval {
54          $values = $self->config($class);                  my $class = $self->class;
55          warn "# $class config = ",dump( $values );                  $self->load_class( $class );
56    
57          if ( @required ) {                  if ( $html = $self->params_form ) {
58                  $html = qq|<h1>Required params for $class</h1><form method="post">|;                          warn "got params form for $class";
59                  foreach my $attr ( @required ) {                  } else {
60                          my $name = $attr->name;                          my $o;
61                          my $type = $name =~ m/^pass/ ? 'password' : 'text';                          # we don't want default status elements
62                          my $value =                          $self->params->{status} = [];
63                                  $values ? $values->{$name} :                          $o = $class->new( %{ $self->params } );
64                                  $attr->has_default ? $attr->default( $name ) :                          $o->depends if $o->can('depends');
65                                  '';  
66  #warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 );                          if ( $self->run eq 'markup' ) {
67                          $html .= qq|<label for="$name">$name</label><input type="$type" name="$name" value="$value">|;                                  warn "## using ",ref($o), "->markup";
68                  }                                  $html = $o->markup;
69                  $html .= qq|<input type="submit" value="Run $class"></form>|;                                  # preserve status
70          } else {                                  push @{ $self->status }, { $class => $o->status } if $o->can('status');
71                  my $o = $class->new( %{ $self->params } );  
72                  $o->depends if $o->can('depends');                                  warn ">>> markup $class ",length( $html ), " bytes\n";
73                  if ( $o->can('markup') ) {                          } elsif ( $self->run eq 'sponge' ) {
74                          warn "## using ",ref($o), "->markup";                                  my $data = $o->sponge;
75                          $html = eval { $o->markup };                                  confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
76                          if ( $@ ) {                                  my $rows = $#{ $data->{rows} } + 1;
77                                  warn $@;                                  $rows ||= 'no';
78                                  $html .= qq{<code>$@</code>};                                  $html .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>";
79                                    $html .= '<table>';
80                                    $html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
81                                    $html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
82                                    $html .= '</table>';
83                            } elsif ( $self->run =~ m{^data(\.(js|json|yaml|yml))?$} ) {
84                                    my $data = $o->data;
85                                    if ( my $format = $1 ) {
86                                            $html .= to_json( $data ) if $format =~ m{js(on)?};
87                                            $html .= Dump( $data )    if $format =~ m{yaml?};
88                                    }
89                                    if ( ! $html ) {
90                                            $html .= Frey::Dumper->new( data => $data )->markup;
91                                            push @{ $self->status }, { 'Dump' => $data };
92                                    }
93                            } else {
94                                    $html = $self->error( "IGNORE: $class ", $o->dump );
95                          }                          }
96                          warn ">>> markup $class ",length( $html ), " bytes\n";                  };
97                  } elsif ( $o->can('data') ) {  
98                          my $data = $o->data;                  if ( ref($html) eq 'HASH' ) {
99                          $html .= Frey::Dumper->new( data => $data )->markup;                          $html = $self->page( %$html );
                         $html .= '<hr/><code>' . $self->html_dump( $data ) . '</code>';  
100                  } else {                  } else {
101                          $html = "IGNORE: $class ", $o->dump;                          $html = $self->page( title => $self->class, body => $html );
102                          warn $html;                  };
103                  }          };
104          }  
105            $html = $self->error( $@ ) if $@;
106    
107          return $self->page( title => $class, body => $html );          return $html;
108  }  }
109    
110  1;  1;

Legend:
Removed from v.327  
changed lines
  Added in v.400

  ViewVC Help
Powered by ViewVC 1.1.26