1 |
package Frey::Run; |
2 |
use Moose; |
3 |
extends 'Frey'; |
4 |
with 'Frey::Web'; |
5 |
with 'Frey::Config'; |
6 |
with 'Frey::Escape'; |
7 |
|
8 |
=head1 NAME |
9 |
|
10 |
Frey::Run - display required form field for Class and run it |
11 |
|
12 |
=head1 DESCRIPTION |
13 |
|
14 |
This object will try to run other Moose objects from your application. It |
15 |
will try to invoke C<data>, and C<markup> method on the. |
16 |
|
17 |
=cut |
18 |
|
19 |
sub runnable { qw/data markup/ } |
20 |
|
21 |
has 'class' => ( |
22 |
is => 'rw', |
23 |
isa => 'Str', |
24 |
required => 1, |
25 |
); |
26 |
|
27 |
has 'params' => ( |
28 |
is => 'rw', |
29 |
isa => 'HashRef', |
30 |
default => sub { {} }, |
31 |
); |
32 |
|
33 |
use Data::Dump qw/dump/; |
34 |
|
35 |
sub html { |
36 |
my ( $self ) = @_; |
37 |
|
38 |
my $class = $self->class; |
39 |
|
40 |
my @required = |
41 |
grep { |
42 |
defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } ) |
43 |
} |
44 |
map { |
45 |
my $attr = $class->meta->get_attribute($_); |
46 |
$attr->is_required && $attr; |
47 |
} $class->meta->get_attribute_list; |
48 |
|
49 |
warn "## required = ",dump( map { $_->name } @required ), " for $class"; |
50 |
|
51 |
my $html; |
52 |
my $values = {}; |
53 |
$values = $self->config($class) if $self->can('config'); |
54 |
|
55 |
if ( @required ) { |
56 |
$html = qq|<h1>Required params for $class</h1><form method="post">|; |
57 |
foreach my $attr ( @required ) { |
58 |
my $name = $attr->name; |
59 |
my $type = $name =~ m/^pass/ ? 'password' : 'text'; |
60 |
my $value = |
61 |
$values ? $values->{$name} : |
62 |
$attr->has_default ? $attr->default( $name ) : |
63 |
''; |
64 |
#warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 ); |
65 |
$html .= qq|<label for="$name">$name</label><input type="$type" name="$name" value="$value">|; |
66 |
} |
67 |
$html .= qq|<input type="submit" value="Run $class"></form>|; |
68 |
} else { |
69 |
my $o = $class->new( %{ $self->params } ); |
70 |
$o->depends if $o->can('depends'); |
71 |
if ( $o->can('markup') ) { |
72 |
warn "## using ",ref($o), "->markup"; |
73 |
$html = eval { $o->markup }; |
74 |
if ( $@ ) { |
75 |
warn $@; |
76 |
$html .= qq{<code>$@</code>}; |
77 |
} |
78 |
warn ">>> markup $class ",length( $html ), " bytes\n"; |
79 |
} elsif ( $o->can('data') ) { |
80 |
$html = '<code>' . $self->html_escape( dump( $o->data ) ) . '</code>'; |
81 |
} else { |
82 |
$html = "IGNORE: $class ", $o->dump; |
83 |
warn $html; |
84 |
} |
85 |
} |
86 |
|
87 |
return $self->page( title => $class, body => $html ); |
88 |
} |
89 |
|
90 |
1; |