2 |
use Moose; |
use Moose; |
3 |
#extends 'Frey::ClassLoader'; |
#extends 'Frey::ClassLoader'; |
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/; |
28 |
|
|
29 |
subtype 'Runnable' |
subtype 'Runnable' |
30 |
=> as 'Str', |
=> as 'Str', |
31 |
=> where sub { m{^as_} }; |
=> where sub { m{^as_} || m{_as_} }; |
32 |
|
|
33 |
sub formats_available { qw/html js json yaml yml/ } |
sub formats_available { qw/html js json yaml yml/ } |
34 |
enum 'Formats' => formats_available; |
enum 'Formats' => formats_available; |
65 |
my $class = $self->class; |
my $class = $self->class; |
66 |
$self->load_class( $class ); |
$self->load_class( $class ); |
67 |
|
|
68 |
if ( $body = $self->params_form ) { |
if ( my $form = $self->params_form ) { |
69 |
|
$html = $self->page( body => $form ); |
70 |
warn "got required params form for $class ", $self->run, " format: ", $self->format; |
warn "got required params form for $class ", $self->run, " format: ", $self->format; |
71 |
} else { |
} else { |
72 |
|
|
73 |
$self->usage->{ $class }++; |
$self->usage->{ $class }++; |
74 |
|
|
75 |
|
=begin remove |
76 |
|
|
77 |
my $o; |
my $o; |
78 |
my ( $meta, $is_role, $instance ) = $self->class_meta( $class ); |
my ( $meta, $is_role, $instance ) = $self->class_meta( $class ); |
79 |
if ( $is_role ) { |
if ( $is_role ) { |
80 |
$o = $instance; |
$o = $instance; |
81 |
|
if ( $o->can('add_status') ) { |
82 |
|
$self->TODO("missing add_status in $o"); |
83 |
|
Frey::Web->meta->apply( $o ); |
84 |
|
warn "# apply Frey::Web to $class instance $o"; |
85 |
|
} |
86 |
} else { |
} else { |
87 |
$o = $self->new_frey_class( $class, $self->params ); |
$o = $self->new_frey_class( $class, $self->params ); |
88 |
} |
} |
89 |
|
=cut |
90 |
|
|
91 |
|
my $o = $self->new_frey_class( $class, $self->params ); |
92 |
$o->depends if $o->can('depends'); |
$o->depends if $o->can('depends'); |
93 |
|
|
94 |
my @status; |
if ( $self->run =~ m{as_markup} ) { |
95 |
|
$html = $o->page( run => $self->run ); |
96 |
push @status, { $self->editor( $class ) => $self->params } if $o->can('status'); |
} elsif ( $self->run =~ m{as_sponge} ) { |
|
|
|
|
if ( $self->run eq 'as_markup' && ! $o->can('page') ) { |
|
|
warn "## using ",ref($o), "->as_markup"; |
|
|
$body = $o->as_markup unless $html; |
|
|
warn ">>> markup $class ",length( $body ), " ", $html ? 'html' : 'body', " bytes"; |
|
|
} elsif ( $self->run eq 'as_sponge' ) { |
|
97 |
$data = $o->as_sponge; |
$data = $o->as_sponge; |
98 |
confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH'; |
confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH'; |
99 |
if ( $self->format eq 'html' ) { |
if ( $self->format eq 'html' ) { |
105 |
$body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} }; |
$body .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} }; |
106 |
$body .= '</table>'; |
$body .= '</table>'; |
107 |
} |
} |
108 |
} elsif ( $self->run eq 'as_data' ) { |
} elsif ( $self->run =~ m{as_data} ) { |
109 |
$data = $o->as_data; |
my $run = $self->run; |
110 |
|
$data = $o->$run; |
111 |
|
confess "no data for $class->$run" unless defined $data; |
112 |
|
$self->add_status( { $self->run => $data } ); |
113 |
} else { |
} else { |
114 |
$body = $self->error( "IGNORE: $class ", $o->dump ); |
$body = $self->error( "IGNORE: $class ", $o->dump ); |
115 |
} |
} |
117 |
if ( defined $data ) { |
if ( defined $data ) { |
118 |
$html .= to_json( $data ) if $self->format =~ m{js(on)?}; |
$html .= to_json( $data ) if $self->format =~ m{js(on)?}; |
119 |
$html .= Dump( $data ) if $self->format =~ m{ya?ml}; |
$html .= Dump( $data ) if $self->format =~ m{ya?ml}; |
|
push @status, { 'data' => $data }; |
|
120 |
} |
} |
121 |
if ( ! $html ) { |
if ( ! $html ) { |
122 |
$body = Frey::View::Dumper->new( data => $body )->as_markup if ref $body; |
$body = Frey::View::Dumper->new( data => $body )->as_markup if ref $body; |
123 |
$body .= Frey::View::Dumper->new( data => $data )->as_markup if defined $data; |
$body .= Frey::View::Dumper->new( data => $data )->as_markup if defined $data; |
124 |
} |
} |
125 |
|
|
126 |
warn "## status from $self ",dump(@status); |
$o->title( $class ); |
127 |
|
|
128 |
if ( $self->run eq 'as_markup' && $o->can('page') ) { |
$html = $o->page( body => $body ) if $body && !$html; |
129 |
$o->add_status($_) foreach @status; |
|
130 |
$html = $o->page; |
confess "no html output for $class ", $o->dump unless defined $html; |
|
warn "got ", length($html), "for page"; |
|
|
} else { |
|
|
$self->add_status($_) foreach @status; |
|
|
} |
|
131 |
}; |
}; |
132 |
|
|
|
if ( ref($body) eq 'HASH' ) { |
|
|
$html = $self->page( %$body ); |
|
|
warn "WARNING: old calling convention with HASH which is depriciated but produced ", length($html), " bytes"; |
|
|
} elsif ( $body && ! $html ) { |
|
|
$html = $self->page( title => $self->class . ' run', body => $body ); |
|
|
warn "wrap body of ",length($body), " in page with ", length($html), " bytes"; |
|
|
}; |
|
133 |
}; |
}; |
134 |
|
|
135 |
warn "## status ", dump( map { keys %$_ } @{ $self->status } ); |
$self->status_parts; |
136 |
|
|
137 |
|
if ( $@ ) { |
138 |
|
my $error = $@; |
139 |
|
my $o = Frey->new; |
140 |
|
Frey::Web->meta->apply( $o ); |
141 |
|
$html = $o->page( body => $self->error( $error ) ); |
142 |
|
} |
143 |
|
|
144 |
$html = $self->page( title => $self->class, body => $self->error( $@ ) ) if $@; |
warn $self->class, " produced ", length($html), " bytes"; |
145 |
|
|
146 |
return $html; |
return $html; |
147 |
} |
} |