1 |
package Frey::Run; |
package Frey::Run; |
2 |
use Moose; |
use Moose; |
3 |
#extends 'Frey::ClassLoader'; |
#extends 'Frey::Class::Loader'; |
4 |
extends 'Frey::Action'; |
extends 'Frey::Action'; |
|
with 'Frey::Web'; |
|
|
with 'Frey::Escape'; |
|
5 |
with 'Frey::Session'; |
with 'Frey::Session'; |
6 |
|
|
7 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
|
use Frey::View::Dumper; |
|
8 |
use JSON; |
use JSON; |
9 |
use YAML; |
use YAML; |
10 |
|
|
11 |
|
use lib 'lib'; |
12 |
|
use Frey::View::Dumper; |
13 |
|
|
14 |
=head1 NAME |
=head1 NAME |
15 |
|
|
16 |
Frey::Run - display required form field for Class and run it |
Frey::Run - display required form field for Class and run it |
27 |
=cut |
=cut |
28 |
|
|
29 |
use Moose::Util::TypeConstraints; |
use Moose::Util::TypeConstraints; |
30 |
|
use Frey::Class::Loader; # class_runnable_re |
31 |
|
|
32 |
subtype 'Runnable' |
subtype 'Runnable' |
33 |
=> as 'Str', |
=> as 'Str', |
34 |
=> where sub { m{^as_} }; |
=> where sub { Frey::Class::Loader::class_runnable_re }; |
35 |
|
|
36 |
sub formats_available { qw/html js json yaml yml/ } |
sub formats_available { qw/html js json yaml yml/ } |
37 |
enum 'Formats' => formats_available; |
enum 'Formats' => formats_available; |
60 |
default => 'html', |
default => 'html', |
61 |
); |
); |
62 |
|
|
63 |
|
has 'request_url' => ( |
64 |
|
documentation => 'Take url from params if not specified', |
65 |
|
is => 'rw', |
66 |
|
isa => 'Uri', coerce => 1, |
67 |
|
lazy => 1, |
68 |
|
default => sub { |
69 |
|
my $self = shift; |
70 |
|
$self->params->{request_url}; |
71 |
|
}, |
72 |
|
); |
73 |
|
|
74 |
sub html { |
sub html { |
75 |
my ( $self ) = @_; |
my ( $self ) = @_; |
76 |
|
|
77 |
my ($html,$body,$data); |
my ($html,$body,$data); |
78 |
|
|
79 |
|
my $current_status; |
80 |
|
$current_status->{$_}++ foreach $self->status; |
81 |
|
|
82 |
eval { |
eval { |
83 |
my $class = $self->class; |
my $class = $self->class; |
84 |
$self->load_class( $class ); |
$self->load_class( $class ); |
85 |
|
|
86 |
if ( $body = $self->params_form ) { |
$self->add_usage( $class ); |
87 |
warn "got required params form for $class ", $self->run, " format: ", $self->format; |
|
88 |
|
if ( my $form = $self->params_form ) { |
89 |
|
$html = $self->html_page( body => $form ); |
90 |
|
warn "missing required params form for $class ", $self->run, " format: ", $self->format; |
91 |
} else { |
} else { |
92 |
|
|
93 |
$self->usage->{ $class }++; |
=begin remove |
94 |
|
|
95 |
my $o; |
my $o; |
96 |
my ( $meta, $is_role, $instance ) = $self->class_meta( $class ); |
my ( $meta, $is_role, $instance ) = $self->class_meta( $class ); |
97 |
if ( $is_role ) { |
if ( $is_role ) { |
98 |
$o = $instance; |
$o = $instance; |
99 |
|
if ( $o->can('add_status') ) { |
100 |
|
$self->TODO("missing add_status in $o"); |
101 |
|
Frey::Web->meta->apply( $o ); |
102 |
|
warn "# apply Frey::Web to $class instance $o"; |
103 |
|
} |
104 |
} else { |
} else { |
105 |
$o = $self->new_frey_class( $class, $self->params ); |
$o = $self->new_frey_class( $class, $self->params ); |
106 |
} |
} |
107 |
|
=cut |
108 |
|
|
109 |
|
my $o = $self->new_frey_class( $class, $self->params ); |
110 |
$o->depends if $o->can('depends'); |
$o->depends if $o->can('depends'); |
111 |
|
|
112 |
push @{ $self->status }, { qq|<a target="editor" href="/editor+$class+1">$class</a>| => $self->params }; |
if ( $self->run =~ m{as_markup} ) { |
113 |
|
$html = $o->html_page( run => $self->run ); |
114 |
if ( $self->run eq 'as_markup' ) { |
} elsif ( $self->run =~ m{(.*as_sponge)} ) { |
115 |
warn "## using ",ref($o), "->as_markup"; |
$data = $o->$1; |
|
if ( $o->can('page') ) { |
|
|
$html = $o->page; |
|
|
} |
|
|
$body = $o->as_markup unless $html; |
|
|
|
|
|
warn ">>> markup $class ",length( $html || $body ), " ", $html ? 'html' : 'body', " bytes"; |
|
|
} elsif ( $self->run eq 'as_sponge' ) { |
|
|
$data = $o->as_sponge; |
|
116 |
confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH'; |
confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH'; |
117 |
if ( $self->format eq 'html' ) { |
if ( $self->format eq 'html' ) { |
118 |
my $rows = $#{ $data->{rows} } + 1; |
my $rows = $#{ $data->{rows} } + 1; |
122 |
$body .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>'; |
$body .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>'; |
123 |
$body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} }; |
$body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} }; |
124 |
$body .= '</table>'; |
$body .= '</table>'; |
125 |
|
|
126 |
|
$self->add_css(qq| |
127 |
|
tr:nth-child(even) { |
128 |
|
background-color: #eee; |
129 |
|
} |
130 |
|
|); |
131 |
|
|
132 |
|
delete( $data->{rows} ); # too much dumplication |
133 |
|
$body .= Frey::View::Dumper->new( data => $data )->as_markup if $data; |
134 |
} |
} |
135 |
} elsif ( $self->run eq 'as_data' ) { |
} elsif ( $self->run =~ m{(as_data|sql)} ) { |
136 |
$data = $o->as_data; |
my $run = $self->run; |
137 |
|
$data = $o->$run; |
138 |
|
confess "no data for $class->$run" unless defined $data; |
139 |
|
$self->add_status( { $self->run => $data } ); |
140 |
} else { |
} else { |
141 |
$body = $self->error( "IGNORE: $class ", $o->dump ); |
$body = $self->error( "IGNORE: $class ", $o->dump ); |
142 |
} |
} |
144 |
if ( defined $data ) { |
if ( defined $data ) { |
145 |
$html .= to_json( $data ) if $self->format =~ m{js(on)?}; |
$html .= to_json( $data ) if $self->format =~ m{js(on)?}; |
146 |
$html .= Dump( $data ) if $self->format =~ m{ya?ml}; |
$html .= Dump( $data ) if $self->format =~ m{ya?ml}; |
|
push @{ $self->status }, { 'data' => $data }; |
|
147 |
} |
} |
148 |
if ( ! $html ) { |
if ( ! $html ) { |
149 |
$body .= Frey::View::Dumper->new( data => $data )->as_markup; |
$body = Frey::View::Dumper->new( data => $body )->as_markup if ref $body; |
150 |
|
$body .= Frey::View::Dumper->new( data => $data )->as_markup if defined $data && ! defined $body; |
151 |
} |
} |
152 |
|
|
153 |
# override our status with one from object |
$o->title( $class ); |
154 |
eval { |
|
155 |
$self->status( $o->status ); |
$html = $o->html_page( body => $body ) if $body && !$html; |
156 |
}; |
$self->content_type( $o->content_type ); |
|
warn "can't override status: $@" if $@; |
|
|
}; |
|
157 |
|
|
158 |
|
confess "no html output for $class ", $o->dump unless defined $html; |
159 |
|
|
160 |
|
if ( $o->can('status') ) { |
161 |
|
foreach ( $o->status ) { |
162 |
|
next if $current_status->{$_}++; |
163 |
|
$self->add_status( $_ ); |
164 |
|
# warn "# run add_status: ", $self->dump( $_ ); # FIXME |
165 |
|
} |
166 |
|
} |
167 |
|
|
|
if ( ref($body) eq 'HASH' ) { |
|
|
$html = $self->page( %$body ); |
|
|
} elsif ( $body && ! $html ) { |
|
|
$html = $self->page( title => $self->class . ' run', body => $body ); |
|
168 |
}; |
}; |
169 |
|
|
170 |
}; |
}; |
171 |
|
|
172 |
$html = $self->page( title => $self->class, body => $self->error( $@ ) ) if $@; |
$self->status_parts; |
173 |
|
|
174 |
|
if ( $@ ) { |
175 |
|
my $error = $@; |
176 |
|
warn $error; |
177 |
|
|
178 |
|
exit if $error =~ m{Attempt to reload \S+ aborted}; # FIXME Mojo can't reload DBIx::Class |
179 |
|
|
180 |
|
my $o = Frey->new; |
181 |
|
$o->{request_url} = $self->request_url; # sigh, this is dynamic languge, right? |
182 |
|
Frey::Web->meta->apply( $o ); |
183 |
|
$html = $o->html_page( body => $self->error( $error, undef ) ); |
184 |
|
} |
185 |
|
|
186 |
|
warn $self->class, " produced ", length($html), " bytes of ", $self->content_type; |
187 |
|
|
188 |
return $html; |
return $html; |
189 |
} |
} |
190 |
|
|
191 |
|
__PACKAGE__->meta->make_immutable; |
192 |
|
no Moose; |
193 |
|
no Moose::Util::TypeConstraints; |
194 |
|
|
195 |
1; |
1; |