1 |
package Frey::Run; |
package Frey::Run; |
2 |
use Moose; |
use Moose; |
3 |
extends 'Frey'; |
#extends 'Frey::ClassLoader'; |
4 |
|
extends 'Frey::PPI'; |
5 |
with 'Frey::Web'; |
with 'Frey::Web'; |
6 |
|
with 'Frey::Config'; |
7 |
|
with 'Frey::Escape'; |
8 |
|
|
9 |
|
use Data::Dump qw/dump/; |
10 |
|
use Frey::Dumper; |
11 |
|
|
12 |
=head1 NAME |
=head1 NAME |
13 |
|
|
16 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
17 |
|
|
18 |
This object will try to run other Moose objects from your application. It |
This object will try to run other Moose objects from your application. It |
19 |
will try to invoke C<html> or C<markup> method on the. |
will try to invoke C<data>, and C<markup> method on the. |
20 |
|
|
21 |
=cut |
=cut |
22 |
|
|
23 |
|
sub runnable { qw/data markup sponge/ } |
24 |
|
|
25 |
has 'class' => ( |
has 'class' => ( |
26 |
is => 'rw', |
is => 'rw', |
27 |
isa => 'Str', |
isa => 'Str', |
28 |
required => 1, |
required => 1, |
29 |
); |
); |
30 |
|
|
31 |
use Data::Dump qw/dump/; |
has 'params' => ( |
32 |
|
is => 'rw', |
33 |
|
isa => 'HashRef', |
34 |
|
default => sub { {} }, |
35 |
|
); |
36 |
|
|
37 |
sub html { |
sub html { |
38 |
my ( $self, $req ) = @_; |
my ( $self ) = @_; |
39 |
|
|
|
my %params = $req->params; |
|
40 |
my $class = $self->class; |
my $class = $self->class; |
41 |
|
|
42 |
|
$self->load_class( $class ); |
43 |
|
|
44 |
my @required = |
my @required = |
45 |
grep { |
grep { |
46 |
defined $_ && !defined( $params{$_} ) |
defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } ) |
47 |
} |
} |
48 |
map { |
map { |
49 |
my $attr = $class->meta->get_attribute($_); |
my $attr = $class->meta->get_attribute($_); |
50 |
$attr->is_required && $_ |
$attr->is_required && $attr; |
51 |
} $class->meta->get_attribute_list; |
} $class->meta->get_attribute_list; |
52 |
|
|
53 |
warn "## required = ",dump( @required ); |
warn "## required = ",dump( map { $_->name } @required ), " for $class"; |
|
warn "## params = ",dump( %params ); |
|
54 |
|
|
55 |
my $html; |
my $html; |
56 |
|
my $values = {}; |
57 |
|
$values = $self->config($class); |
58 |
|
warn "# $class config = ",dump( $values ); |
59 |
|
|
60 |
if ( @required ) { |
if ( @required ) { |
61 |
$html = qq|<h1>Required params for $class</h1><form method="post">|; |
$html = qq|<h1>$class params</h1><form method="post">|; |
62 |
$html .= qq|<label for="$_">$_</label><input type="text" name="$_">| foreach @required; |
|
63 |
|
my $a; |
64 |
|
my @attrs = map { $a->{$_}++; $_ } $self->attribute_order; |
65 |
|
push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @required; |
66 |
|
warn "# attrs = ",dump( @attrs ); |
67 |
|
|
68 |
|
foreach my $name ( @attrs ) { |
69 |
|
my $attr = $class->meta->get_attribute( $name ); |
70 |
|
my $type = $name =~ m/^pass/ ? 'password' : 'text'; |
71 |
|
my $value = |
72 |
|
$values ? $values->{$name} : |
73 |
|
$attr->has_default ? $attr->default( $name ) : |
74 |
|
''; |
75 |
|
#warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 ); |
76 |
|
$html .= qq|<label for="$name">$name</label><input type="$type" name="$name" value="$value">|; |
77 |
|
} |
78 |
$html .= qq|<input type="submit" value="Run $class"></form>|; |
$html .= qq|<input type="submit" value="Run $class"></form>|; |
79 |
} else { |
} else { |
80 |
my $o = $class->new( %params ); |
my $o = $class->new( %{ $self->params } ); |
81 |
$o->depends if $o->can('depends'); |
$o->depends if $o->can('depends'); |
82 |
if ( $o->can('html') ) { |
if ( $o->can('markup') ) { |
83 |
warn "## turning over to $o->html"; |
warn "## using ",ref($o), "->markup"; |
84 |
$o->html( $req ); |
$html = eval { $o->markup }; |
85 |
} elsif ( $o->can('markup') ) { |
if ( $@ ) { |
86 |
warn "## using $o->markup"; |
warn $@; |
87 |
$html = $o->markup; |
$html .= qq{<code>$@</code>}; |
88 |
|
} |
89 |
warn ">>> markup $class ",length( $html ), " bytes\n"; |
warn ">>> markup $class ",length( $html ), " bytes\n"; |
90 |
|
} elsif ( $o->can('sponge') ) { |
91 |
|
my $data = $o->sponge; |
92 |
|
$html .= '<table>'; |
93 |
|
$html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>'; |
94 |
|
$html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} }; |
95 |
|
$html .= '</table>'; |
96 |
|
} elsif ( $o->can('data') ) { |
97 |
|
my $data = $o->data; |
98 |
|
$html .= Frey::Dumper->new( data => $data )->markup; |
99 |
|
$html .= '<hr/><code>' . $self->html_dump( $data ) . '</code>'; |
100 |
} else { |
} else { |
101 |
$html = "IGNORE: $class ", $o->dump; |
$html = "IGNORE: $class ", $o->dump; |
102 |
warn $html; |
warn $html; |
103 |
} |
} |
104 |
} |
} |
105 |
|
|
106 |
$req->print( $self->page( title => $class, body => $html ) ); |
return $self->page( title => $class, body => $html ); |
107 |
} |
} |
108 |
|
|
109 |
1; |
1; |