1 |
package Frey::Run; |
2 |
use Moose; |
3 |
#extends 'Frey::ClassLoader'; |
4 |
extends 'Frey::Action'; |
5 |
with 'Frey::Web'; |
6 |
with 'Frey::Escape'; |
7 |
|
8 |
use Data::Dump qw/dump/; |
9 |
use Frey::Dumper; |
10 |
|
11 |
=head1 NAME |
12 |
|
13 |
Frey::Run - display required form field for Class and run it |
14 |
|
15 |
=head1 DESCRIPTION |
16 |
|
17 |
This object will try to run other Moose objects from your application. It |
18 |
will try to invoke C<data>, and C<markup> method on the. |
19 |
|
20 |
=head1 SEE ALSO |
21 |
|
22 |
L<Frey::Action> which creates form for params |
23 |
|
24 |
=cut |
25 |
|
26 |
use Moose::Util::TypeConstraints; |
27 |
|
28 |
enum 'Runnable' => qw/data markup sponge/; |
29 |
|
30 |
sub runnable { qw/data markup sponge/ } |
31 |
|
32 |
has 'class' => ( |
33 |
is => 'rw', |
34 |
isa => 'Str', |
35 |
required => 1, |
36 |
); |
37 |
|
38 |
has 'params' => ( |
39 |
is => 'rw', |
40 |
isa => 'HashRef', |
41 |
default => sub { {} }, |
42 |
); |
43 |
|
44 |
has 'run' => ( |
45 |
is => 'rw', |
46 |
isa => 'Runnable', |
47 |
default => 'markup', |
48 |
); |
49 |
|
50 |
sub html { |
51 |
my ( $self ) = @_; |
52 |
|
53 |
my $html; |
54 |
eval { |
55 |
my $class = $self->class; |
56 |
$self->load_class( $class ); |
57 |
|
58 |
if ( $html = $self->params_form ) { |
59 |
warn "got params form for $class"; |
60 |
} else { |
61 |
my $o; |
62 |
$o = $class->new( %{ $self->params } ); |
63 |
$o->depends if $o->can('depends'); |
64 |
|
65 |
if ( $self->run eq 'markup' ) { |
66 |
warn "## using ",ref($o), "->markup"; |
67 |
$html = $o->markup; |
68 |
warn ">>> markup $class ",length( $html ), " bytes\n"; |
69 |
} elsif ( $self->run eq 'sponge' ) { |
70 |
my $data = $o->sponge; |
71 |
confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH'; |
72 |
my $rows = $#{ $data->{rows} } + 1; |
73 |
$rows ||= 'no'; |
74 |
$html .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>"; |
75 |
$html .= '<table>'; |
76 |
$html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>'; |
77 |
$html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} }; |
78 |
$html .= '</table>'; |
79 |
} elsif ( $self->run eq 'data' ) { |
80 |
my $data = $o->data; |
81 |
$html .= Frey::Dumper->new( data => $data )->markup; |
82 |
$html .= '<hr/><span class="frey-popdown">dump<span><code>' . $self->html_dump( $data ) . '</code></span></span>'; |
83 |
} else { |
84 |
$html = $self->error( "IGNORE: $class ", $o->dump ); |
85 |
} |
86 |
}; |
87 |
|
88 |
}; |
89 |
$html .= $self->error( $@ ) if $@; |
90 |
|
91 |
return $self->page( %$html ) if ref($html) eq 'HASH'; |
92 |
return $self->page( title => $self->class, body => $html ); |
93 |
} |
94 |
|
95 |
1; |