20 |
|
|
21 |
=cut |
=cut |
22 |
|
|
23 |
sub runnable { qw/data markup/ } |
use Moose::Util::TypeConstraints; |
24 |
|
|
25 |
|
enum 'Runnable' => qw/data markup sponge/; |
26 |
|
|
27 |
|
sub runnable { qw/data markup sponge/ } |
28 |
|
|
29 |
has 'class' => ( |
has 'class' => ( |
30 |
is => 'rw', |
is => 'rw', |
38 |
default => sub { {} }, |
default => sub { {} }, |
39 |
); |
); |
40 |
|
|
41 |
|
has 'run' => ( |
42 |
|
is => 'rw', |
43 |
|
isa => 'Runnable', |
44 |
|
default => 'markup', |
45 |
|
); |
46 |
|
|
47 |
sub html { |
sub html { |
48 |
my ( $self ) = @_; |
my ( $self ) = @_; |
49 |
|
|
69 |
|
|
70 |
if ( @required ) { |
if ( @required ) { |
71 |
$html = qq|<h1>$class params</h1><form method="post">|; |
$html = qq|<h1>$class params</h1><form method="post">|; |
72 |
foreach my $name ( $self->attribute_order ) { |
|
73 |
|
my $a; |
74 |
|
my @attrs = map { $a->{$_}++; $_ } $self->attribute_order; |
75 |
|
push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @required; |
76 |
|
warn "# attrs = ",dump( @attrs ); |
77 |
|
|
78 |
|
foreach my $name ( @attrs ) { |
79 |
my $attr = $class->meta->get_attribute( $name ); |
my $attr = $class->meta->get_attribute( $name ); |
80 |
my $type = $name =~ m/^pass/ ? 'password' : 'text'; |
my $type = $name =~ m/^pass/ ? 'password' : 'text'; |
81 |
my $value = |
my $value = ''; |
82 |
$values ? $values->{$name} : |
my $value_html = ''; |
83 |
$attr->has_default ? $attr->default( $name ) : |
if ( ref($values) eq 'HASH' ) { |
84 |
''; |
$value = $values->{$name}; |
85 |
|
} elsif ( ref($values) eq 'ARRAY' ) { |
86 |
|
$value_html = qq|<select name="$name">| . join("\n", |
87 |
|
map { |
88 |
|
my $v = $_->{$name}; |
89 |
|
qq|<option value="$v">$v</option>| |
90 |
|
} @$values |
91 |
|
) . qq|</select>|; |
92 |
|
} elsif ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) { |
93 |
|
$value_html = qq|<select name="$name">| . join("\n", |
94 |
|
map { qq|<option value="$_">$_</option>| } @{ $attr->type_constraint->values } |
95 |
|
) . qq|</select>|; |
96 |
|
} elsif ( $attr->has_default ) { |
97 |
|
$value = $attr->default( $name ); |
98 |
|
} |
99 |
|
$value_html = qq|<input type="$type" name="$name" value="$value">| unless $value_html; |
100 |
|
|
101 |
#warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 ); |
#warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 ); |
102 |
$html .= qq|<label for="$name">$name</label><input type="$type" name="$name" value="$value">|; |
$html .= qq|<label for="$name">$name</label>| . $value_html; |
103 |
} |
} |
104 |
$html .= qq|<input type="submit" value="Run $class"></form>|; |
$html .= qq|<input type="submit" value="Run $class"></form>|; |
105 |
} else { |
} else { |
106 |
my $o = $class->new( %{ $self->params } ); |
eval { |
107 |
$o->depends if $o->can('depends'); |
my $o; |
108 |
if ( $o->can('markup') ) { |
$o = $class->new( %{ $self->params } ); |
109 |
warn "## using ",ref($o), "->markup"; |
$o->depends if $o->can('depends'); |
110 |
$html = eval { $o->markup }; |
|
111 |
if ( $@ ) { |
if ( $self->run eq 'markup' ) { |
112 |
warn $@; |
warn "## using ",ref($o), "->markup"; |
113 |
$html .= qq{<code>$@</code>}; |
$html = $o->markup; |
114 |
|
warn ">>> markup $class ",length( $html ), " bytes\n"; |
115 |
|
} elsif ( $self->run eq 'sponge' ) { |
116 |
|
my $data = $o->sponge; |
117 |
|
confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH'; |
118 |
|
my $rows = $#{ $data->{rows} } + 1; |
119 |
|
$rows ||= 'no'; |
120 |
|
$html .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>"; |
121 |
|
$html .= '<table>'; |
122 |
|
$html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>'; |
123 |
|
$html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} }; |
124 |
|
$html .= '</table>'; |
125 |
|
} elsif ( $self->run eq 'data' ) { |
126 |
|
my $data = $o->data; |
127 |
|
$html .= Frey::Dumper->new( data => $data )->markup; |
128 |
|
$html .= '<hr/><code>' . $self->html_dump( $data ) . '</code>'; |
129 |
|
} else { |
130 |
|
$html = $self->error( "IGNORE: $class ", $o->dump ); |
131 |
} |
} |
132 |
warn ">>> markup $class ",length( $html ), " bytes\n"; |
}; |
133 |
} elsif ( $o->can('data') ) { |
|
134 |
my $data = $o->data; |
$html .= $self->error( $@ ) if $@; |
|
$html .= Frey::Dumper->new( data => $data )->markup; |
|
|
$html .= '<hr/><code>' . $self->html_dump( $data ) . '</code>'; |
|
|
} else { |
|
|
$html = "IGNORE: $class ", $o->dump; |
|
|
warn $html; |
|
|
} |
|
135 |
} |
} |
136 |
|
|
137 |
|
return $self->page( %$html ) if ref($html) eq 'HASH'; |
138 |
return $self->page( title => $class, body => $html ); |
return $self->page( title => $class, body => $html ); |
139 |
} |
} |
140 |
|
|