1 |
package Frey::Run; |
2 |
use Moose; |
3 |
#extends 'Frey::ClassLoader'; |
4 |
extends 'Frey::Action'; |
5 |
with 'Frey::Web'; |
6 |
with 'Frey::Escape'; |
7 |
with 'Frey::Session'; |
8 |
|
9 |
use Data::Dump qw/dump/; |
10 |
use Frey::Dumper; |
11 |
use JSON; |
12 |
use YAML; |
13 |
|
14 |
=head1 NAME |
15 |
|
16 |
Frey::Run - display required form field for Class and run it |
17 |
|
18 |
=head1 DESCRIPTION |
19 |
|
20 |
This object will try to run other Moose objects from your application. It |
21 |
will try to invoke C<data>, and C<markup> method on the. |
22 |
|
23 |
=head1 SEE ALSO |
24 |
|
25 |
L<Frey::Action> which creates form for params |
26 |
|
27 |
=cut |
28 |
|
29 |
use Moose::Util::TypeConstraints; |
30 |
|
31 |
sub runnable { qw/data data.js markup sponge/ } |
32 |
enum 'Runnable' => runnable; |
33 |
|
34 |
sub formats_available { qw/html js json yaml yml/ } |
35 |
enum 'Formats' => formats_available; |
36 |
|
37 |
has 'class' => ( |
38 |
is => 'rw', |
39 |
isa => 'Str', |
40 |
required => 1, |
41 |
); |
42 |
|
43 |
has 'params' => ( |
44 |
is => 'rw', |
45 |
isa => 'HashRef', |
46 |
default => sub { {} }, |
47 |
); |
48 |
|
49 |
has 'run' => ( |
50 |
is => 'rw', |
51 |
isa => 'Runnable', |
52 |
default => 'markup', |
53 |
); |
54 |
|
55 |
has 'format' => ( |
56 |
is => 'rw', |
57 |
isa => 'Formats', |
58 |
default => 'html', |
59 |
); |
60 |
|
61 |
sub html { |
62 |
my ( $self ) = @_; |
63 |
|
64 |
my ($html,$body,$data); |
65 |
eval { |
66 |
my $class = $self->class; |
67 |
$self->load_class( $class ); |
68 |
|
69 |
if ( $body = $self->params_form ) { |
70 |
warn "got required params form for $class ", $self->run, " format: ", $self->format; |
71 |
} else { |
72 |
|
73 |
$self->usage->{ $class }++; |
74 |
|
75 |
my $o; |
76 |
my ( $meta, $is_role, $instance ) = $self->class_meta( $class ); |
77 |
if ( $is_role ) { |
78 |
$o = $instance; |
79 |
} else { |
80 |
$o = $class->new( %{ $self->params } ); |
81 |
} |
82 |
|
83 |
$o->depends if $o->can('depends'); |
84 |
|
85 |
push @{ $self->status }, { qq|<a target="editor" href="/editor+$class+1">$class</a>| => $self->params }; |
86 |
|
87 |
if ( $self->run eq 'markup' ) { |
88 |
warn "## using ",ref($o), "->markup"; |
89 |
if ( $o->can('page') ) { |
90 |
#$html = $o->page; |
91 |
$body = $o->markup unless $html; |
92 |
} else { |
93 |
$body = $o->markup; |
94 |
} |
95 |
|
96 |
warn ">>> markup $class ",length( $html || $body ), " ", $html ? 'html' : 'body', " bytes"; |
97 |
} elsif ( $self->run eq 'sponge' ) { |
98 |
$data = $o->sponge; |
99 |
confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH'; |
100 |
if ( $self->format eq 'html' ) { |
101 |
my $rows = $#{ $data->{rows} } + 1; |
102 |
$rows ||= 'no'; |
103 |
$body .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>"; |
104 |
$body .= '<table>'; |
105 |
$body .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>'; |
106 |
$body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} }; |
107 |
$body .= '</table>'; |
108 |
} |
109 |
} elsif ( $self->run eq 'data' ) { |
110 |
$data = $o->data; |
111 |
} else { |
112 |
$body = $self->error( "IGNORE: $class ", $o->dump ); |
113 |
} |
114 |
|
115 |
if ( defined $data ) { |
116 |
$html .= to_json( $data ) if $self->format =~ m{js(on)?}; |
117 |
$html .= Dump( $data ) if $self->format =~ m{ya?ml}; |
118 |
push @{ $self->status }, { 'data' => $data }; |
119 |
} |
120 |
if ( ! $html ) { |
121 |
$body .= Frey::Dumper->new( data => $data )->markup; |
122 |
} |
123 |
|
124 |
# override our status with one from object |
125 |
eval { |
126 |
$self->status( $o->status ); |
127 |
}; |
128 |
warn "can't override status: $@" if $@; |
129 |
}; |
130 |
|
131 |
|
132 |
if ( ref($body) eq 'HASH' ) { |
133 |
$html = $self->page( %$body ); |
134 |
} elsif ( $body && ! $html ) { |
135 |
$html = $self->page( title => $self->class . ' run', body => $body ); |
136 |
}; |
137 |
}; |
138 |
|
139 |
$html = $self->page( title => $self->class, body => $self->error( $@ ) ) if $@; |
140 |
|
141 |
return $html; |
142 |
} |
143 |
|
144 |
1; |