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'; |
7 |
|
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 |
15 |
|
|
16 |
|
=head1 DESCRIPTION |
17 |
|
|
18 |
|
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. |
20 |
|
|
21 |
=cut |
=cut |
22 |
|
|
23 |
|
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', |
31 |
isa => 'Str', |
isa => 'Str', |
32 |
required => 1, |
required => 1, |
33 |
); |
); |
34 |
|
|
35 |
use Data::Dump qw/dump/; |
has 'params' => ( |
36 |
|
is => 'rw', |
37 |
|
isa => 'HashRef', |
38 |
|
default => sub { {} }, |
39 |
|
); |
40 |
|
|
41 |
|
has 'run' => ( |
42 |
|
is => 'rw', |
43 |
|
isa => 'Runnable', |
44 |
|
); |
45 |
|
|
46 |
sub html { |
sub html { |
47 |
my ( $self, $req ) = @_; |
my ( $self ) = @_; |
48 |
|
|
|
my %params = $req->params; |
|
49 |
my $class = $self->class; |
my $class = $self->class; |
50 |
|
|
51 |
|
$self->load_class( $class ); |
52 |
|
|
53 |
my @required = |
my @required = |
54 |
grep { |
grep { |
55 |
defined $_ && !defined( $params{$_} ) |
defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } ) |
56 |
} |
} |
57 |
map { |
map { |
58 |
my $attr = $class->meta->get_attribute($_); |
my $attr = $class->meta->get_attribute($_); |
59 |
$attr->is_required && $_ |
$attr->is_required && $attr; |
60 |
} $class->meta->get_attribute_list; |
} $class->meta->get_attribute_list; |
61 |
|
|
62 |
warn "## required = ",dump( @required ); |
warn "## required = ",dump( map { $_->name } @required ), " for $class"; |
|
warn "## params = ",dump( %params ); |
|
63 |
|
|
64 |
my $html; |
my $html; |
65 |
|
my $values = {}; |
66 |
|
$values = $self->config($class); |
67 |
|
warn "# $class config = ",dump( $values ); |
68 |
|
|
69 |
if ( @required ) { |
if ( @required ) { |
70 |
$html = qq|<h1>Required params for $class</h1><form method="post">|; |
$html = qq|<h1>$class params</h1><form method="post">|; |
71 |
$html .= qq|<label for="$_">$_</label><input type="text" name="$_">| foreach @required; |
|
72 |
|
my $a; |
73 |
|
my @attrs = map { $a->{$_}++; $_ } $self->attribute_order; |
74 |
|
push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @required; |
75 |
|
warn "# attrs = ",dump( @attrs ); |
76 |
|
|
77 |
|
foreach my $name ( @attrs ) { |
78 |
|
my $attr = $class->meta->get_attribute( $name ); |
79 |
|
my $type = $name =~ m/^pass/ ? 'password' : 'text'; |
80 |
|
my $value = ''; |
81 |
|
my $value_html = ''; |
82 |
|
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 ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) { |
92 |
|
$value_html = qq|<select name="$name">| . join("\n", |
93 |
|
map { qq|<option value="$_">$_</option>| } @{ $attr->type_constraint->values } |
94 |
|
) . qq|</select>|; |
95 |
|
} elsif ( $attr->has_default ) { |
96 |
|
$value = $attr->default( $name ); |
97 |
|
} |
98 |
|
$value_html = qq|<input type="$type" name="$name" value="$value">| unless $value_html; |
99 |
|
|
100 |
|
#warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 ); |
101 |
|
$html .= qq|<label for="$name">$name</label>| . $value_html; |
102 |
|
} |
103 |
$html .= qq|<input type="submit" value="Run $class"></form>|; |
$html .= qq|<input type="submit" value="Run $class"></form>|; |
104 |
} else { |
} else { |
105 |
my $o = $class->new( %params ); |
my $o = $class->new( %{ $self->params } ); |
106 |
$o->depends if $o->can('depends'); |
$o->depends if $o->can('depends'); |
107 |
$html = $o->markup; |
if ( $o->can('markup') ) { |
108 |
|
warn "## using ",ref($o), "->markup"; |
109 |
|
$html = eval { $o->markup }; |
110 |
|
$html .= $self->error( $@ ) if $@; |
111 |
|
warn ">>> markup $class ",length( $html ), " bytes\n"; |
112 |
|
} elsif ( $o->can('sponge') ) { |
113 |
|
my $data = $o->sponge; |
114 |
|
$html .= '<table>'; |
115 |
|
$html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>'; |
116 |
|
$html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} }; |
117 |
|
$html .= '</table>'; |
118 |
|
} elsif ( $o->can('data') ) { |
119 |
|
my $data = $o->data; |
120 |
|
$html .= Frey::Dumper->new( data => $data )->markup; |
121 |
|
$html .= '<hr/><code>' . $self->html_dump( $data ) . '</code>'; |
122 |
|
} else { |
123 |
|
$html = "IGNORE: $class ", $o->dump; |
124 |
|
warn $html; |
125 |
|
} |
126 |
} |
} |
127 |
|
|
128 |
warn ">>> markup $class ",length( $html ), " bytes\n"; |
return $self->page( title => $class, body => $html ); |
|
$req->print( $self->page( title => $class, body => $html ) ); |
|
129 |
} |
} |
130 |
|
|
131 |
1; |
1; |