/[Frey]/trunk/lib/Frey/Run.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Frey/Run.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 326 - (show annotations)
Thu Nov 6 20:25:27 2008 UTC (15 years, 5 months ago) by dpavlin
File size: 2280 byte(s)
use Frey::Dumper for data calls
1 package Frey::Run;
2 use Moose;
3 extends 'Frey::ClassLoader';
4 with 'Frey::Web';
5 with 'Frey::Config';
6 with 'Frey::Escape';
7
8 use Data::Dump qw/dump/;
9 use Frey::Dumper;
10
11 =head1 NAME
12
13 Frey::Run - display required form field for Class and run it
14
15 =head1 DESCRIPTION
16
17 This object will try to run other Moose objects from your application. It
18 will try to invoke C<data>, and C<markup> method on the.
19
20 =cut
21
22 sub runnable { qw/data markup/ }
23
24 has 'class' => (
25 is => 'rw',
26 isa => 'Str',
27 required => 1,
28 );
29
30 has 'params' => (
31 is => 'rw',
32 isa => 'HashRef',
33 default => sub { {} },
34 );
35
36 sub html {
37 my ( $self ) = @_;
38
39 my $class = $self->class;
40
41 $self->load_class( $class );
42
43 my @required =
44 grep {
45 defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } )
46 }
47 map {
48 my $attr = $class->meta->get_attribute($_);
49 $attr->is_required && $attr;
50 } $class->meta->get_attribute_list;
51
52 warn "## required = ",dump( map { $_->name } @required ), " for $class";
53
54 my $html;
55 my $values = {};
56 $values = $self->config($class);
57 warn "# $class config = ",dump( $values );
58
59 if ( @required ) {
60 $html = qq|<h1>Required params for $class</h1><form method="post">|;
61 foreach my $attr ( @required ) {
62 my $name = $attr->name;
63 my $type = $name =~ m/^pass/ ? 'password' : 'text';
64 my $value =
65 $values ? $values->{$name} :
66 $attr->has_default ? $attr->default( $name ) :
67 '';
68 #warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 );
69 $html .= qq|<label for="$name">$name</label><input type="$type" name="$name" value="$value">|;
70 }
71 $html .= qq|<input type="submit" value="Run $class"></form>|;
72 } else {
73 my $o = $class->new( %{ $self->params } );
74 $o->depends if $o->can('depends');
75 if ( $o->can('markup') ) {
76 warn "## using ",ref($o), "->markup";
77 $html = eval { $o->markup };
78 if ( $@ ) {
79 warn $@;
80 $html .= qq{<code>$@</code>};
81 }
82 warn ">>> markup $class ",length( $html ), " bytes\n";
83 } elsif ( $o->can('data') ) {
84 my $data = $o->data;
85 $html .= Frey::Dumper->new( data => $data )->markup;
86 $html .= '<hr/><code>' . $self->html_escape( dump( $data ) ) . '</code>';
87 } else {
88 $html = "IGNORE: $class ", $o->dump;
89 warn $html;
90 }
91 }
92
93 return $self->page( title => $class, body => $html );
94 }
95
96 1;

  ViewVC Help
Powered by ViewVC 1.1.26