/[Frey]/trunk/lib/Frey/Action.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/Action.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 480 - (show annotations)
Thu Nov 20 14:39:43 2008 UTC (15 years, 5 months ago) by dpavlin
File size: 4342 byte(s)
create editor links
1 package Frey::Action;
2 use Moose;
3 extends 'Frey::PPI';
4 with 'Frey::Web';
5 with 'Frey::Config';
6
7 use Clone qw/clone/;
8 use Data::Dump qw/dump/;
9
10 =head1 DESCRIPTION
11
12 Invoke any L<Frey> object creating html for with various default parameters
13 if not supplied at invocation.
14
15 =cut
16
17 has 'class' => (
18 is => 'rw',
19 isa => 'Str',
20 required => 1,
21 );
22
23 has 'params' => (
24 is => 'rw',
25 isa => 'HashRef',
26 default => sub { {} },
27 );
28
29 =head2 required
30
31 my @required_attributes = $self->required;
32 my $required_attributes = $self->required;
33
34 =cut
35
36 sub required {
37 my ( $self ) = @_;
38 $self->load_class( $self->class );
39 my @required =
40 grep {
41 defined $_ && $_->can('name') &&
42 ! defined( $self->params->{ $_->name } ) &&
43 ! $_->is_lazy
44 }
45 map {
46 my $attr = $self->class->meta->get_attribute($_);
47 blessed $attr && $attr->is_required && $attr;
48 } $self->class->meta->get_attribute_list;
49
50 warn "## required = ",dump( map { $_->name } @required ), " for ", $self->class if @required && $self->debug;
51 return @required if wantarray;
52 return \@required;
53 }
54
55 =head2 attributes
56
57 Generated from attributes specified in code (extracted using L<Frey::PPI>)
58 and required atributes
59
60 my @class_attributes = $self->attributes;
61 my @class_attributes = $self->attributes;
62
63 =cut
64
65 sub attributes {
66 my ( $self ) = @_;
67 my $a;
68 my @attrs = @{ $self->attribute_order };
69 @attrs = map { $a->{$_}++; $_ } @attrs;
70 push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @{ $self->required };
71 warn "# attributes = ",dump( @attrs ) if $self->debug;
72 return @attrs if wantarray;
73 return \@attrs;
74 }
75
76 =head2 params_form
77
78 my $html = $self->params_form;
79 my ($html,$default_params) = $self->params_form;
80
81 =cut
82
83 sub params_form {
84 my ( $self ) = @_;
85 my @required = $self->required;
86 if ( ! @required ) {
87 warn "all params available ", dump( $self->params ), " not creating form" if $self->debug;
88 return (undef,$self->params) if wantarray;
89 return;
90 } else {
91 warn $self->class, " required params ", map { $_->dump(2) } @required if $self->debug;
92 }
93
94 my $class = $self->class;
95
96 $self->load_class( $class );
97
98 my $default = clone $self->params; # XXX we really don't want to modify params!
99
100 my $config_params = {};
101 $config_params = $self->config($class);
102 warn "# $class config = ",dump( $config_params ) if $self->debug;
103
104 my $form;
105
106 foreach my $name ( grep { ! $class->meta->get_attribute($_)->is_lazy } $self->attributes ) {
107 my $attr_type = '';
108 my $type = $name =~ m/^pass/ ? 'password' : 'text';
109 my $label = $name;
110 my $value = '';
111 my $label_title = '';
112 my $value_html = '';
113 if ( ref($config_params) eq 'HASH' ) {
114 $value = $config_params->{$name};
115 } elsif ( ref($config_params) eq 'ARRAY' ) {
116 $value_html = qq|<select name="$name">| . join("\n",
117 map {
118 my $v = $_->{$name};
119 qq|<option value="$v">$v</option>|
120 } @$config_params
121 ) . qq|</select>|;
122 $default->{$name} = $config_params->[0]->{$name};
123 } elsif ( my $attr = $class->meta->get_attribute( $name ) ) {
124 if ( $attr->has_type_constraint ) {
125 $attr_type = $attr->type_constraint->name;
126 if ( $attr->type_constraint->can('values') ) {
127 $value_html = qq|<select name="$name">| . join("\n",
128 map { qq|<option value="$_">$_</option>| } @{ $attr->type_constraint->values }
129 ) . qq|</select>|;
130 } elsif ( $attr_type !~ m{^(Str|Int)$} ) {
131 $value_html = qq|<textarea name="$name" title="$attr_type">$value</textarea>|;
132 }
133 }
134 $value = $attr->default( $name ) if ! $value && $attr->has_default;
135 $label_title = qq| title="| . $attr->documentation . qq|"| if $attr->has_documentation;
136 } else {
137 warn "wired attribute $name";
138 }
139
140 $default->{$name} = $value unless defined $default->{$name};
141
142 $value_html = qq|<input type="$type" name="$name" title="$attr_type" value="$value">| unless $value_html;
143
144 # warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 );
145 $form .= qq|<label for="$name"$label_title>$label</label>| . $value_html;
146 }
147 my $html = qq|<h1>$class params</h1><form method="post">$form<input type="submit" value="Run $class"></form>|;
148 push @{ $self->status }, {
149 $self->editor( $self->class ) => {
150 params => $self->params,
151 config_params => $config_params,
152 default => $default
153 },
154 };
155
156 return ($html,$default) if wantarray;
157 return $html;
158 }
159
160 1;

  ViewVC Help
Powered by ViewVC 1.1.26