/[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

Annotation of /trunk/lib/Frey/Action.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26