1 |
package Frey::Run; |
package Frey::Run; |
2 |
use Moose; |
use Moose; |
3 |
#extends 'Frey::ClassLoader'; |
#extends 'Frey::ClassLoader'; |
4 |
extends 'Frey::PPI'; |
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/; |
9 |
use Frey::Dumper; |
use Frey::Dumper; |
10 |
|
use JSON; |
11 |
|
|
12 |
=head1 NAME |
=head1 NAME |
13 |
|
|
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>, and C<markup> method on the. |
will try to invoke C<data>, and C<markup> method on the. |
20 |
|
|
21 |
|
=head1 SEE ALSO |
22 |
|
|
23 |
|
L<Frey::Action> which creates form for params |
24 |
|
|
25 |
=cut |
=cut |
26 |
|
|
27 |
use Moose::Util::TypeConstraints; |
use Moose::Util::TypeConstraints; |
28 |
|
|
29 |
enum 'Runnable' => qw/data markup sponge/; |
sub runnable { qw/data data.js markup sponge/ } |
30 |
|
enum 'Runnable' => runnable; |
31 |
|
|
32 |
sub runnable { qw/data markup sponge/ } |
sub formats_available { qw/html js json yaml yml/ } |
33 |
|
enum 'Formats' => formats_available; |
34 |
|
|
35 |
has 'class' => ( |
has 'class' => ( |
36 |
is => 'rw', |
is => 'rw', |
47 |
has 'run' => ( |
has 'run' => ( |
48 |
is => 'rw', |
is => 'rw', |
49 |
isa => 'Runnable', |
isa => 'Runnable', |
50 |
|
default => 'markup', |
51 |
|
); |
52 |
|
|
53 |
|
has 'format' => ( |
54 |
|
is => 'rw', |
55 |
|
isa => 'Formats', |
56 |
|
default => 'html', |
57 |
); |
); |
58 |
|
|
59 |
sub html { |
sub html { |
60 |
my ( $self ) = @_; |
my ( $self ) = @_; |
61 |
|
|
62 |
my $class = $self->class; |
my ($html,$body,$data); |
63 |
|
eval { |
64 |
|
my $class = $self->class; |
65 |
|
$self->load_class( $class ); |
66 |
|
|
67 |
|
if ( $body = $self->params_form ) { |
68 |
|
warn "got required params form for $class ", $self->run, " format: ", $self->format; |
69 |
|
} else { |
70 |
|
|
71 |
|
my $o = $class->new( %{ $self->params } ); |
72 |
|
$o->depends if $o->can('depends'); |
73 |
|
|
74 |
$self->load_class( $class ); |
push @{ $self->status }, { qq|<a target="editor" href="/editor+$class+1">$class</a>| => $self->params }; |
75 |
|
|
76 |
my @required = |
if ( $self->run eq 'markup' ) { |
77 |
grep { |
warn "## using ",ref($o), "->markup"; |
78 |
defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } ) |
if ( $o->can('page') ) { |
79 |
} |
$html = $o->page; |
80 |
map { |
$body = $o->markup unless $html; |
81 |
my $attr = $class->meta->get_attribute($_); |
} else { |
82 |
$attr->is_required && $attr; |
$body = $o->markup; |
83 |
} $class->meta->get_attribute_list; |
} |
84 |
|
|
85 |
warn "## required = ",dump( map { $_->name } @required ), " for $class"; |
warn ">>> markup $class ",length( $html || $body ), " ", $html ? 'html' : 'body', " bytes"; |
86 |
|
} elsif ( $self->run eq 'sponge' ) { |
87 |
my $html; |
$data = $o->sponge; |
88 |
my $values = {}; |
confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH'; |
89 |
$values = $self->config($class); |
if ( $self->format eq 'html' ) { |
90 |
warn "# $class config = ",dump( $values ); |
my $rows = $#{ $data->{rows} } + 1; |
91 |
|
$rows ||= 'no'; |
92 |
if ( @required ) { |
$body .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>"; |
93 |
$html = qq|<h1>$class params</h1><form method="post">|; |
$body .= '<table>'; |
94 |
|
$body .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>'; |
95 |
my $a; |
$body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} }; |
96 |
my @attrs = map { $a->{$_}++; $_ } $self->attribute_order; |
$body .= '</table>'; |
97 |
push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @required; |
} |
98 |
warn "# attrs = ",dump( @attrs ); |
} elsif ( $self->run eq 'data' ) { |
99 |
|
$data = $o->data; |
100 |
foreach my $name ( @attrs ) { |
} else { |
101 |
my $attr = $class->meta->get_attribute( $name ); |
$body = $self->error( "IGNORE: $class ", $o->dump ); |
|
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>|; |
|
|
} elsif ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) { |
|
|
$value_html = qq|<select name="$name">| . join("\n", |
|
|
map { qq|<option value="$_">$_</option>| } @{ $attr->type_constraint->values } |
|
|
) . qq|</select>|; |
|
|
} elsif ( $attr->has_default ) { |
|
|
$value = $attr->default( $name ); |
|
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 ); |
if ( defined $data ) { |
105 |
$html .= qq|<label for="$name">$name</label>| . $value_html; |
$html .= to_json( $data ) if $self->format =~ m{js(on)?}; |
106 |
} |
$html .= Dump( $data ) if $self->format =~ m{ya?ml}; |
107 |
$html .= qq|<input type="submit" value="Run $class"></form>|; |
push @{ $self->status }, { 'data' => $data }; |
108 |
} else { |
} |
109 |
my $o = $class->new( %{ $self->params } ); |
if ( ! $html ) { |
110 |
$o->depends if $o->can('depends'); |
$body .= Frey::Dumper->new( data => $data )->markup; |
111 |
if ( $o->can('markup') ) { |
} |
112 |
warn "## using ",ref($o), "->markup"; |
|
113 |
$html = eval { $o->markup }; |
# override our status with one from object |
114 |
$html .= $self->error( $@ ) if $@; |
$self->status( $o->status ) if $o->can('status') && $o->status; |
115 |
warn ">>> markup $class ",length( $html ), " bytes\n"; |
}; |
116 |
} elsif ( $o->can('sponge') ) { |
|
117 |
my $data = $o->sponge; |
|
118 |
$html .= '<table>'; |
if ( ref($body) eq 'HASH' ) { |
119 |
$html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>'; |
$html = $self->page( %$body ); |
120 |
$html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} }; |
} elsif ( $body && ! $html ) { |
121 |
$html .= '</table>'; |
$html = $self->page( title => $self->class . ' run', body => $body ); |
122 |
} elsif ( $o->can('data') ) { |
}; |
123 |
my $data = $o->data; |
}; |
124 |
$html .= Frey::Dumper->new( data => $data )->markup; |
|
125 |
$html .= '<hr/><code>' . $self->html_dump( $data ) . '</code>'; |
$html = $self->page( title => $self->class, body => dump($html) . $self->error( $@ ) ) if $@; |
|
} else { |
|
|
$html = "IGNORE: $class ", $o->dump; |
|
|
warn $html; |
|
|
} |
|
|
} |
|
126 |
|
|
127 |
return $self->page( title => $class, body => $html ); |
return $html; |
128 |
} |
} |
129 |
|
|
130 |
1; |
1; |