/[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 285 by dpavlin, Wed Nov 5 08:20:57 2008 UTC revision 364 by dpavlin, Sun Nov 16 19:50:36 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::PPI';
5  with 'Frey::Web';  with 'Frey::Web';
6  with 'Frey::Config';  with 'Frey::Config';
7  with 'Frey::Escape';  with 'Frey::Escape';
8    
9    use Data::Dump qw/dump/;
10    use Frey::Dumper;
11    
12  =head1 NAME  =head1 NAME
13    
14  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 16  Frey::Run - display required form field
16  =head1 DESCRIPTION  =head1 DESCRIPTION
17    
18  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
19  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.
20    
21  =cut  =cut
22    
23  sub execute { qw/data markup/ }  use Moose::Util::TypeConstraints;
24    
25    enum 'Runnable' => qw/data markup sponge/;
26    
27    sub runnable { qw/data markup sponge/ }
28    
29  has 'class' => (  has 'class' => (
30          is => 'rw',          is => 'rw',
# Line 30  has 'params' => ( Line 38  has 'params' => (
38          default => sub { {} },          default => sub { {} },
39  );  );
40    
41  use Data::Dump qw/dump/;  has 'run' => (
42            is => 'rw',
43            isa => 'Runnable',
44            default => 'markup',
45    );
46    
47  sub html {  sub html {
48          my ( $self ) = @_;          my ( $self ) = @_;
49    
50          my $class = $self->class;          my $class = $self->class;
51    
52            $self->load_class( $class );
53    
54          my @required =          my @required =
55                  grep {                  grep {
56                          defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } )                          defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } )
# Line 50  sub html { Line 64  sub html {
64    
65          my $html;          my $html;
66          my $values = {};          my $values = {};
67          my $values = $self->config($class) if $self->can('config');          $values = $self->config($class);
68            warn "# $class config = ",dump( $values );
69    
70          if ( @required ) {          if ( @required ) {
71                  $html = qq|<h1>Required params for $class</h1><form method="post">|;                  $html = qq|<h1>$class params</h1><form method="post">|;
72                  foreach my $attr ( @required ) {  
73                          my $name = $attr->name;                  my $a;
74                    my @attrs = map {  $a->{$_}++; $_ } @{ $self->attribute_order };
75                    push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @required;
76                    warn "# attrs = ",dump( @attrs );
77    
78                    foreach my $name ( @attrs ) {
79                          my $type = $name =~ m/^pass/ ? 'password' : 'text';                          my $type = $name =~ m/^pass/ ? 'password' : 'text';
80                          my $value =                          my $value = '';
81                                  $values ? $values->{$name} :                          my $value_html = '';
82                                  $attr->has_default ? $attr->default( $name ) :                          if ( ref($values) eq 'HASH' ) {
83                                  '';                                  $value = $values->{$name};
84                            } elsif ( ref($values) eq 'ARRAY' ) {
85                                    $value_html = qq|<select name="$name">| . join("\n",
86                                            map {
87                                                    my $v = $_->{$name};
88                                                    qq|<option value="$v">$v</option>|
89                                            } @$values
90                                    ) . qq|</select>|;
91                            } elsif ( my $attr = $class->meta->get_attribute( $name ) ) {
92                                    if ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) {
93                                            $value_html = qq|<select name="$name">| . join("\n",
94                                                    map { qq|<option value="$_">$_</option>| } @{ $attr->type_constraint->values }
95                                            ) . qq|</select>|;
96                                    } elsif ( $attr->has_default ) {
97                                            $value = $attr->default( $name );
98                                    }
99                            } else {
100                                    warn "wired attribute $name";
101                            }
102                            $value_html = qq|<input type="$type" name="$name" value="$value">| unless $value_html;
103    
104  #warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 );  #warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 );
105                          $html .= qq|<label for="$name">$name</label><input type="$type" name="$name" value="$value">|;                          $html .= qq|<label for="$name">$name</label>| . $value_html;
106                  }                  }
107                  $html .= qq|<input type="submit" value="Run $class"></form>|;                  $html .= qq|<input type="submit" value="Run $class"></form>|;
108          } else {          } else {
109                  my $o = $class->new( %{ $self->params } );                  eval {
110                  $o->depends if $o->can('depends');                          my $o;
111                  if ( $o->can('markup') ) {                          $o = $class->new( %{ $self->params } );
112                          warn "## using ",ref($o), "->markup";                          $o->depends if $o->can('depends');
113                          $html = eval { $o->markup };  
114                          if ( $@ ) {                          if ( $self->run eq 'markup' ) {
115                                  warn $@;                                  warn "## using ",ref($o), "->markup";
116                                  $html .= qq{<code>$@</code>};                                  $html = $o->markup;
117                                    warn ">>> markup $class ",length( $html ), " bytes\n";
118                            } elsif ( $self->run eq 'sponge' ) {
119                                    my $data = $o->sponge;
120                                    confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
121                                    my $rows = $#{ $data->{rows} } + 1;
122                                    $rows ||= 'no';
123                                    $html .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>";
124                                    $html .= '<table>';
125                                    $html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
126                                    $html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
127                                    $html .= '</table>';
128                            } elsif ( $self->run eq 'data' ) {
129                                    my $data = $o->data;
130                                    $html .= Frey::Dumper->new( data => $data )->markup;
131                                    $html .= '<hr/><span class="frey-popdown">dump<span><code>' . $self->html_dump( $data ) . '</code></span></span>';
132                            } else {
133                                    $html = $self->error( "IGNORE: $class ", $o->dump );
134                          }                          }
135                          warn ">>> markup $class ",length( $html ), " bytes\n";                  };
136                  } elsif ( $o->can('data') ) {  
137                          $html = '<code>' . $self->html_escape( dump( $o->data ) ) . '</code>';                  $html .= $self->error( $@ ) if $@;
                 } else {  
                         $html = "IGNORE: $class ", $o->dump;  
                         warn $html;  
                 }  
138          }          }
139    
140            return $self->page( %$html ) if ref($html) eq 'HASH';
141          return $self->page( title => $class, body => $html );          return $self->page( title => $class, body => $html );
142  }  }
143    

Legend:
Removed from v.285  
changed lines
  Added in v.364

  ViewVC Help
Powered by ViewVC 1.1.26