/[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 414 - (show annotations)
Tue Nov 18 14:15:45 2008 UTC (15 years, 5 months ago) by dpavlin
File size: 4248 byte(s)
decrease output without debug
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') && !defined( $self->params->{ $_->name } )
42 }
43 map {
44 my $attr = $self->class->meta->get_attribute($_);
45 $attr->is_required && $attr;
46 } $self->class->meta->get_attribute_list;
47
48 warn "## required = ",dump( map { $_->name } @required ), " for ", $self->class if @required && $self->debug;
49 return @required if wantarray;
50 return \@required;
51 }
52
53 =head2 attributes
54
55 Generated from attributes specified in code (extracted using L<Frey::PPI>)
56 and required atributes
57
58 my @class_attributes = $self->attributes;
59 my @class_attributes = $self->attributes;
60
61 =cut
62
63 sub attributes {
64 my ( $self ) = @_;
65 my $a;
66 my @attrs = @{ $self->attribute_order };
67 @attrs = map { $a->{$_}++; $_ } @attrs;
68 push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @{ $self->required };
69 warn "# attributes = ",dump( @attrs ) if $self->debug;
70 return @attrs if wantarray;
71 return \@attrs;
72 }
73
74 =head2 params_form
75
76 my $html = $self->params_form;
77 my ($html,$default_params) = $self->params_form;
78
79 =cut
80
81 sub params_form {
82 my ( $self ) = @_;
83 my @required = $self->required;
84 if ( ! @required ) {
85 warn "all params available ", dump( $self->params ), " not creating form" if $self->debug;
86 return (undef,$self->params) if wantarray;
87 return;
88 } else {
89 warn $self->class, " required params ", map { $_->dump(2) } @required if $self->debug;
90 }
91
92 my $class = $self->class;
93
94 $self->load_class( $class );
95
96 my $default = clone $self->params; # XXX we really don't want to modify params!
97
98 my $config_params = {};
99 $config_params = $self->config($class);
100 warn "# $class config = ",dump( $config_params ) if $self->debug;
101
102 my $form;
103
104 foreach my $name ( grep { ! $class->meta->get_attribute($_)->is_lazy } $self->attributes ) {
105 my $attr_type = '';
106 my $type = $name =~ m/^pass/ ? 'password' : 'text';
107 my $label = $name;
108 my $value = '';
109 my $label_title = '';
110 my $value_html = '';
111 if ( ref($config_params) eq 'HASH' ) {
112 $value = $config_params->{$name};
113 } elsif ( ref($config_params) eq 'ARRAY' ) {
114 $value_html = qq|<select name="$name">| . join("\n",
115 map {
116 my $v = $_->{$name};
117 qq|<option value="$v">$v</option>|
118 } @$config_params
119 ) . qq|</select>|;
120 $default->{$name} = $config_params->[0]->{$name};
121 } elsif ( my $attr = $class->meta->get_attribute( $name ) ) {
122 if ( $attr->has_type_constraint ) {
123 $attr_type = $attr->type_constraint->name;
124 if ( $attr->type_constraint->can('values') ) {
125 $value_html = qq|<select name="$name">| . join("\n",
126 map { qq|<option value="$_">$_</option>| } @{ $attr->type_constraint->values }
127 ) . qq|</select>|;
128 } elsif ( $attr_type !~ m{^(Str|Int)$} ) {
129 $value_html = qq|<textarea name="$name" title="$attr_type">$value</textarea>|;
130 }
131 }
132 $value = $attr->default( $name ) if ! $value && $attr->has_default;
133 $label_title = qq| title="| . $attr->documentation . qq|"| if $attr->has_documentation;
134 } else {
135 warn "wired attribute $name";
136 }
137
138 $value_html = qq|<input type="$type" name="$name" title="$attr_type" value="$value">| unless $value_html;
139
140 $default->{$name} = $value unless defined $default->{$name};
141
142 # warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 );
143 $form .= qq|<label for="$name"$label_title>$label</label>| . $value_html;
144 }
145 my $html = qq|<h1>$class params</h1><form method="post">$form<input type="submit" value="Run $class"></form>|;
146 push @{ $self->status }, { 'Params' =>
147 {
148 'Config' => $config_params,
149 'Default' => $default
150 },
151 };
152
153 return ($html,$default) if wantarray;
154 return $html;
155 }
156
157 1;

  ViewVC Help
Powered by ViewVC 1.1.26