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

  ViewVC Help
Powered by ViewVC 1.1.26