File Coverage

blib/lib/SyForm.pm
Criterion Covered Total %
statement 35 38 92.1
branch 1 4 25.0
condition 1 3 33.3
subroutine 11 12 91.6
pod 0 3 0.0
total 48 60 80.0


line stmt bran cond sub pod time code
1             package SyForm;
2             BEGIN {
3 7     7   400215 $SyForm::AUTHORITY = 'cpan:GETTY';
4             }
5             # ABSTRACT: Easy form management
6             $SyForm::VERSION = '0.103';
7 7     7   4031 use Moo;
  7         79334  
  7         33  
8 7     7   13264 use Tie::IxHash;
  7         31512  
  7         221  
9 7     7   50 use Module::Runtime qw( use_module );
  7         13  
  7         45  
10 7     7   3058 use SyForm::Exception;
  7         22  
  7         3653  
11              
12             with($_) for (qw(
13             MooX::Traits
14             SyForm::Role::Process
15             SyForm::Role::Label
16             ));
17              
18             #######################
19             #
20             # Class Default Config
21             #
22             #######################
23              
24             has name => (
25             is => 'ro',
26             predicate => 1,
27             );
28              
29             has fields_list => (
30             is => 'ro',
31             init_arg => 'fields',
32             required => 1,
33             );
34              
35             has fields => (
36             is => 'lazy',
37             init_arg => undef,
38             );
39 14     14 0 9618 sub field { shift->fields->FETCH(@_) }
40              
41             sub _build_fields {
42 7     7   4175 my ( $self ) = @_;
43 7         34 my $fields = Tie::IxHash->new;
44 7         190 eval {
45 7         17 my $fields_list = Tie::IxHash->new(@{$self->fields_list});
  7         53  
46 7         525 for my $name ($fields_list->Keys) {
47 27         9383 my %field_args = %{$fields_list->FETCH($name)};
  27         69  
48 27         241 $fields->Push($name, $self->new_field($name, %field_args));
49             }
50             };
51 7 50       2427 SyForm->throw( UnknownErrorOnBuildFields => $self, $@ ) if $@;
52 7         48 return $fields;
53             }
54              
55             has field_names => (
56             is => 'lazy',
57             );
58              
59             sub _build_field_names {
60 2     2   207 my ( $self ) = @_;
61 2         37 return [map { $_->name } $self->fields->Values];
  9         59  
62             }
63              
64             has field_class => (
65             is => 'lazy',
66             );
67              
68 7     7   92 sub _build_field_class { return 'SyForm::Field' }
69              
70             has loaded_field_class => (
71             is => 'lazy',
72             );
73              
74             sub _build_loaded_field_class {
75 7     7   85 my ( $self ) = @_;
76 7         134 return use_module($self->field_class);
77             }
78              
79             sub new_field {
80 27     27 0 62 my ( $self, $name, %field_args ) = @_;
81 27         44 my $field;
82 27   33     528 my $class = delete $field_args{class} || $self->loaded_field_class;
83 27         646 return $class->new(
84             syform => $self,
85             name => $name,
86             %field_args,
87             );
88             }
89              
90             sub throw {
91 0     0 0   my ( $class, $exception, @args ) = @_;
92 0 0         SyForm::Exception->throw($exception) if scalar(@args) == 0;
93 0           use_module('SyForm::Exception::'.$exception)->throw_with_args(@args);
94             }
95              
96             1;
97              
98             __END__
99              
100             =pod
101              
102             =head1 NAME
103              
104             SyForm - Easy form management
105              
106             =head1 VERSION
107              
108             version 0.103
109              
110             =head1 SYNOPSIS
111              
112             use SyForm;
113              
114             my $form = SyForm->new( fields => [
115             'username' => {
116             required => 1,
117             label => 'Your name',
118             html => 'text',
119             },
120             'age' => {
121             decimal => 1,
122             label => 'Your age',
123             html => 'text',
124             },
125             'unchecked' => {
126             label => 'Unchecked',
127             html => 'textarea',
128             },
129             ]);
130              
131             my $view = $form->process( username => 'YoCoolCopKiller', age => 13 );
132              
133             # or ...
134             # $values = $form->process_values(%args);
135             # my $value = $values->value;
136             # !$values->can('success'); # values are only the input
137             # $results = $form->process_results(%args);
138             # my $result = $results->get_result('username');
139             # my $value = $results->values->get_value('username');
140             # my $success = $result->success # result is after check
141              
142             for my $field_name (@{$view->field_names}) {
143             my $input_value = $view->field($field_name)->value;
144             if ($view->success) {
145             my $verified_result = $view->field($field_name)->result;
146             } else {
147             # result is filled for all valid fields, even on invalid form
148             my $verified_result_if_exist = $view->field($field_name)->result;
149             }
150             # for access to the main SyForm::Field of the view field
151             my $syform_field = $view->field($field_name)->field;
152             }
153              
154             $view->html_render; # get HTML
155              
156             =head1 DESCRIPTION
157              
158             B<Deprecated> Please do not use!
159              
160             =head1 AUTHOR
161              
162             Torsten Raudssus <torsten@raudss.us>
163              
164             =head1 COPYRIGHT AND LICENSE
165              
166             This software is copyright (c) 2014 by Torsten Raudssus.
167              
168             This is free software; you can redistribute it and/or modify it under
169             the same terms as the Perl 5 programming language system itself.
170              
171             =cut