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>, C<html> or C<markup> method on the. |
16 |
|
17 |
=cut |
18 |
|
19 |
sub execute { qw/data markup request/ } |
20 |
|
21 |
has 'class' => ( |
22 |
is => 'rw', |
23 |
isa => 'Str', |
24 |
required => 1, |
25 |
); |
26 |
|
27 |
use Data::Dump qw/dump/; |
28 |
|
29 |
sub request { |
30 |
my ( $self, $req ) = @_; |
31 |
|
32 |
my %params = $req->params; |
33 |
my $class = $self->class; |
34 |
|
35 |
my @required = |
36 |
grep { |
37 |
defined $_ && !defined( $params{$_} ) |
38 |
} |
39 |
map { |
40 |
my $attr = $class->meta->get_attribute($_); |
41 |
$attr->is_required && $_ |
42 |
} $class->meta->get_attribute_list; |
43 |
|
44 |
warn "## required = ",dump( @required ); |
45 |
warn "## params = ",dump( %params ); |
46 |
|
47 |
my $html; |
48 |
my $values = $self->config($class); |
49 |
$values = {} if $@; |
50 |
|
51 |
if ( @required ) { |
52 |
$html = qq|<h1>Required params for $class</h1><form method="post">|; |
53 |
foreach my $name ( @required ) { |
54 |
my $type = $name =~ m/^pass/ ? 'password' : 'text'; |
55 |
my $value = $values ? $values->{$name} : ''; |
56 |
$html .= qq|<label for="$name">$name</label><input type="$type" name="$name" value="$value">|; |
57 |
} |
58 |
$html .= qq|<input type="submit" value="Run $class"></form>|; |
59 |
} else { |
60 |
my $o = $class->new( %params ); |
61 |
$o->depends if $o->can('depends'); |
62 |
if ( $o->can('request') ) { |
63 |
warn "## turning over to $o->request"; |
64 |
$o->request( $req ); |
65 |
} elsif ( $o->can('markup') ) { |
66 |
warn "## using ",ref($o), "->markup"; |
67 |
$html = $o->markup; |
68 |
warn ">>> markup $class ",length( $html ), " bytes\n"; |
69 |
} elsif ( $o->can('data') ) { |
70 |
$html = '<code>' . $self->html_escape( dump( $o->data ) ) . '</code>'; |
71 |
} else { |
72 |
$html = "IGNORE: $class ", $o->dump; |
73 |
warn $html; |
74 |
} |
75 |
} |
76 |
|
77 |
$req->print( $self->page( title => $class, body => $html ) ); |
78 |
} |
79 |
|
80 |
1; |