blib/lib/Catalyst/Plugin/HTML/Widget.pm untested

filter: Statementstmt 0.0% 0 / 30 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 65 CC 6 SCAR 25.0
CC
6
Cov
0%
widget
20.0
widget_result
12.0
BEGIN
2.0
CRAP 12.2
1 package Catalyst::Plugin::HTML::Widget;
2  
3 use warnings;
4 use strict;
5 use HTML::Widget;
6  
7 our $VERSION = '1.1';
8  
9 =head1 NAME
10  
11 Catalyst::Plugin::HTML::Widget - HTML Widget And Validation Framework
12  
13 =head1 SYNOPSIS
14  
15 use Catalyst qw/HTML::Widget;
16  
17 $c->widget('foo')->method('get')->action('/foo/action');
18  
19 $c->widget('foo')->element( 'Textfield', 'age' )->label('Age')->size(3);
20 $c->widget('foo')->element( 'Textfield', 'name' )->label('Name')->size(60);
21 $c->widget('foo')->element( 'Submit', 'ok' )->value('OK');
22  
23 $c->widget('foo')->constraint( 'Integer', 'age' )->message('No integer.');
24 $c->widget('foo')->constraint( 'All', 'age', 'name' )
25 ->message('Missing value.');
26  
27 $c->widget( HTML::Widget->new('foo') );
28  
29 $c->widget('foo')->indicator('some_field');
30  
31 my $result = $c->widget('foo')->process;
32 my $result = $c->widget('foo')->process($query);
33  
34 my $result = $c->widget_result('foo'); # with query params
35  
36 =head1 DESCRIPTION
37  
38 HTML Widget And Validation Framework.
39  
40 =head1 METHODS
41  
42 =head2 $c->widget( $name, $widget )
43  
44 Returns a L<HTML::Widget>. If no object exists, it will be created on the
45 fly. The widget name defaults to C<_widget>.
46  
47 =cut
48  
49 sub widget {
50 my ( $c, $name, $widget ) = @_;
51 $widget = $name if ref $name;
52 $c->{_widget} ||= {};
53 $name ||= $widget ? $widget->name ? $widget->name : '_widget' : '_widget';
54 $c->{_widget}->{$name} ||= ( $widget || HTML::Widget->new($name) );
55 return $c->{_widget}->{$name};
56 }
57  
58 =head2 $c->widget_result($name)
59  
60 Returns a L<HTML::Widget::Result> object, processed B<with> query and upload
61 parameters.
62  
63 =cut
64  
65 sub widget_result {
66 my $c = shift;
67 my $w = $c->widget(@_);
68  
69 my $result;
70 my $indi = $w->indicator;
71 $indi = ref $indi ? $indi : sub { $c->request->param( $w->indicator ) };
72 if ( $indi->() ) {
73 local $w->{query} = $c->request;
74 local $w->{uploads} = $c->request->uploads;
75 $result = $w->result;
76 }
77 else {
78 local $w->{query} = undef;
79 local $w->{uploads} = undef;
80 $result = $w->result;
81 }
82  
83 return $result;
84 }
85  
86 =head1 SEE ALSO
87  
88 L<Catalyst>, L<HTML::Widget>
89  
90 =head1 AUTHOR
91  
92 Sebastian Riedel, C<sri@oook.de>
93  
94 =head1 LICENSE
95  
96 This library is free software, you can redistribute it and/or modify it under
97 the same terms as Perl itself.
98  
99 =cut
100  
101 1;