lib/Form/Sensible/Field.pm

filter: Statementstmt 90.0% 108 / 120 Branchbran 63.8% 23 / 36 Conditioncond 63.6% 7 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 91.3% 21 / 23 Podpod 77.7% 7 / 9 totaltotal 82.0% 169 / 206 CC 25 SCAR 14.9
CC
25
Cov
83%
required
12.0
validate_with_coderef
7.5
flatten
7.2
CRAP 4.4
« lib/Form/Sensible/Field/SubForm.pm ↑ Summary lib/Form/Sensible/Renderer/HTML.pm »
1 package Form::Sensible::Field;
2  
3 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Class::MOP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Form::Sensible::DelegateConnection;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 16 has 'name' => (
12 is => 'rw',
13 isa => 'Str',
14 required => 1,
15 );
16  
17 has 'display_name' => (
18 is => 'rw',
19 isa => 'Str',
20 required => 1,
21 32 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
22 32 my $name = ucfirst(shift->name());
23 32 $name =~ s/_/ /g;
24 32 return $name;
25 },
26 16 lazy => 1,
27 );
28  
29 16 has 'field_type' => (
30 is => 'rw',
31 isa => 'Str',
32 required => 1,
33 builder => '_default_field_type',
34 lazy => 1
35 );
36  
37 ## validation contains args to the validator that will be used
38 ## by default, the hashref can contain 'regex' - a ref to a
39 ## regex. or 'code' - a code ref. If both are present,
40 ## the regex will be checked first, then if that succeeds
41 ## the coderef will be processed.
42  
43 has 'validation' => (
44 is => 'rw',
45 isa => 'HashRef',
46 required => 1,
47 29 ▶ default => sub { return { required => 0 }; },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 16 lazy => 1,
49 );
50  
51 ## render hints is a hashref that gives hints about rendering
52 ## for the various renderers. for example:
53 ## render_hints->{HTML} = hash containing information about
54 ## how the field should be rendered.
55  
56 16 has 'render_hints' => (
57 is => 'rw',
58 isa => 'HashRef',
59 required => 1,
60 builder => '_default_render_hints',
61 lazy => 1,
62 );
63  
64 ## note the default behavior is to squash arrays if accepts_multiple is not set.
65 ## if you need different behavior in your subclass, you have to override the default
66 ## value delegate.
67  
68 has 'value_delegate' => (
69 is => 'rw',
70 isa => 'Form::Sensible::DelegateConnection',
71 required => 1,
72 44 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 44 my $self = shift;
74 44 my $value = $self->default_value;
75 585 ▶ my $sub = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
76 585 my $caller = shift;
77  
78 585 ▶ if ($#_ > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ > -1) 86 499 585
79 86 ▶ if (ref($_[0]) eq 'ARRAY' && !($caller->accepts_multiple)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY' and not $caller->accepts_multiple) { } 6 80 86
Condition100%3/3
ref $_[0] eq "ARRAY" and not $caller->accepts_multiple
!ll&&!rl&&r
7736
MC/DC100%2/2
ref $_[0] eq "ARRAY" and not $caller->accepts_multiple
ref $_[0] eq "ARRAY" not $caller->accepts_multiple
Truth table100%3/3
A and not(B)
A= ref $_[0] eq "ARRAY"B= not $caller->accepts_multiple
ABresult
0X0
100
111
80 6 $value = $_[0]->[0];
81 } else {
82 80 $value = $_[0];
83 }
84 }
85 585 return $value;
86 44 };
87 44 return FSConnector($sub);
88 },
89 16 lazy => 1,
90 coerce => 1,
91 # additional options
92 );
93  
94 16 has 'accepts_multiple' => (
95 is => 'rw',
96 isa => 'Bool',
97 required => 1,
98 default => 0,
99 );
100  
101 16 has 'editable' => (
102 is => 'rw',
103 isa => 'Bool',
104 required => 1,
105 default => 1,
106 );
107  
108 ## calling format: ($field, $value) return an arrayref containing error messages if invalid or false (scalar) if valid
109 ## it may be help to think of it as asking the delegate 'is this invalid'
110 has 'validation_delegate' => (
111 is => 'rw',
112 isa => 'Form::Sensible::DelegateConnection',
113 required => 1,
114 31 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
115 31 ▶ return FSConnector(sub { return; });
Subroutine __ANON__: called CC=1 SCAR=0.0
116 },
117 16 lazy => 1,
118 coerce => 1,
119 # additional options
120 );
121  
122 16 has 'default_value' => (
123 is => 'rw',
124 );
125  
126 17 ▶sub _default_field_type {
Subroutine _default_field_type: called CC=1 SCAR=0.0
127 17 my $self = shift;
128  
129 17 my $class = ref($self);
130 17 $class =~ m/::([^:]*)$/;
131 17 return lc($1);
132 }
133  
134 11 ▶sub _default_render_hints {
Subroutine _default_render_hints: called CC=1 SCAR=0.0
135 11 my $self = shift;
136  
137 11 return {};
138 }
139  
140 0 ▶sub required {
Subroutine required: not called CC=3 SCAR=24.8
Pod documented
141 0 my $self = shift;
142  
143 0 ▶ if ($#_ > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ > -1) 0 0 0
144 0 $self->validation->{'required'} = $_[0];
145 }
146 0 ▶ return $self->validation->{'required'} || 0;
Condition0%0/2
$self->validation->{'required'} || 0
l!l
00
MC/DC0%0/1
$self->validation->{'required'} || 0
$self->validation->{'required'}
Truth table0%0/2
A
A= $self->validation->{'required'}
Aresult
11
00
147 }
148  
149 4 ▶sub flatten {
Subroutine flatten: called CC=7 SCAR=19.7
Pod documented
150 4 my ($self, $template_only) = @_;
151  
152 4 my %config = map { $_ => $self->$_ }
153 qw(name display_name default_value field_type render_hints);
154  
155 4 my $class = ref($self);
156 4 ▶ if ($class =~ /^Form::Sensible::Field::(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /^Form::Sensible::Field::(.*)$/) { } 4 0 4
157 4 $class = $1;
158 } else {
159 0 $class = '+' . $class;
160 }
161  
162 4 $config{'field_class'} = $class;
163  
164 4 ▶ if (!$template_only) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($template_only) 4 0 4
165 4 $config{'value'} = $self->value;
166 }
167  
168 4 ▶ if ($self->accepts_multiple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->accepts_multiple) 0 4 4
169 0 $config{'accepts_multiple'} =$self->accepts_multiple;
170 }
171  
172 4 $config{'validation'} = {};
173 4 foreach my $key (keys %{$self->validation}) {
174 4 my $value = $self->validation->{$key};
175 4 ▶ $value = "$value" if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 3 1 4
176 4 $config{'validation'}{$key} = $value;
177 }
178 4 my $additional = $self->get_additional_configuration($template_only);
179 4 foreach my $key (keys %{$additional}) {
180 8 $config{$key} = $additional->{$key};
181 }
182  
183 4 return \%config;
184 }
185  
186 ## hook for adding additional config without having to do 'around' every time.
187 0 ▶sub get_additional_configuration {
Subroutine get_additional_configuration: not called CC=1 SCAR=6.9
Pod documented
188 0 my ($self) = @_;
189  
190 0 return {};
191 }
192  
193 ## built-in field specific validation. Regex and code validation run first.
194 ## by default just calls the validation_delegate.
195 268 ▶sub validate {
Subroutine validate: called CC=5 SCAR=16.2
Pod documented
196 268 my ($self) = @_;
197  
198 268 my @errors;
199  
200 268 ▶ if (defined($self->validation->{'regex'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->validation->{"regex"}) 17 251 268
201 17 push @errors, $self->validate_with_regex($self->validation->{'regex'});
202 }
203 ## if we have a coderef, and we passed regex, run the coderef. Otherwise we
204 ## don't bother.
205 268 ▶ if (defined($self->validation->{'code'}) && $#errors == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->validation->{"code"} and $#errors == -1) 4 264 268
Condition66%2/3
defined $self->validation->{"code"} and $#errors == -1
!ll&&!rl&&r
26404
MC/DC50%1/2
defined $self->validation->{"code"} and $#errors == -1
defined $self->validation->{"code"} $#errors == -1
Truth table66%2/3
A and B
A= defined $self->validation->{"code"}B= $#errors == -1
ABresult
0X0
100
111
206 4 push @errors, $self->validate_with_coderef($self->validation->{'code'});
207 }
208 268 ▶ if (defined($self->validation_delegate)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->validation_delegate) 268 0 268
209 268 push @errors, $self->validation_delegate->($self, $self->value);
210 }
211  
212 268 return grep { defined } @errors;
213  
214 }
215  
216  
217 17 ▶sub validate_with_regex {
Subroutine validate_with_regex: called CC=4 SCAR=13.9
Pod undocumented
218 17 my ($self, $regex) = @_;
219  
220 17 ▶ if (ref($regex) ne 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $regex ne "Regexp") 11 6 17
221 11 $regex = qr/$regex/;
222 }
223 17 ▶ return if $self->value() =~ $regex;
Branch: 1 branch
BranchTrueFalseTotal
if $self->value =~ /$regex/ 12 5 17
224 return exists($self->validation->{'invalid_message'})
225 5 ▶ ? $self->validation->{'invalid_message'}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->validation->{'invalid_message'} ? : 0 5 5
226 : '_FIELDNAME_ is invalid.';
227 }
228  
229 4 ▶sub validate_with_coderef {
Subroutine validate_with_coderef: called CC=5 SCAR=20.2
Pod undocumented
230 4 my ($self, $code) = @_;
231  
232 4 ▶ if (ref($code) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code ne "CODE") 0 4 4
233 0 croak('Bad coderef provided to validate_field_with_coderef');
234 }
235  
236 4 my $results = $code->($self->value(), $self);
237  
238 ## if we get $results of "false" or a message, we return it.
239 ## if we get $results of simply one, we generate the invalid message
240 4 ▶ return $results if ! $results || $results ne "1";
Branch: 1 branch
BranchTrueFalseTotal
if not $results or $results ne 1 4 0 4
Condition66%2/3
not $results or $results ne 1
l!l&&r!l&&!r
310
MC/DC0%0/2
not $results or $results ne 1
not $results $results ne 1
Truth table66%2/3
not(A) or B
A= not $resultsB= $results ne 1
ABresult
1X1
011
000
241 return exists($self->validation->{'invalid_message'})
242 0 ▶ ? $self->validation->{'invalid_message'}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->validation->{'invalid_message'} ? : 0 0 0
243 : '_FIELDNAME_ is invalid.';
244 }
245  
246  
247 1670 ▶sub value {
Subroutine value: called CC=1 SCAR=0.0
Pod documented
248 1670 my $self = shift;
249  
250 1670 return $self->value_delegate->($self,@_);
251 }
252  
253 ## restores a flattened field structure.
254 63 ▶sub create_from_flattened {
Subroutine create_from_flattened: called CC=3 SCAR=11.3
Pod documented
255 63 my ($class, $fieldconfig ) = @_;
256  
257 63 my $fieldclass = $fieldconfig->{'field_class'};
258 63 ▶ if (!$fieldclass) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fieldclass) 0 63 63
259 0 croak "Unable to restore flattened field, no field class defined";
260 }
261 63 my $class_to_load;
262 63 ▶ if ($fieldclass =~ /^\+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fieldclass =~ /^\+(.*)$/) { } 0 63 63
263 0 $class_to_load = $1;
264 } else {
265 63 $class_to_load = 'Form::Sensible::Field::' . $fieldclass;
266 }
267 63 Class::MOP::load_class($class_to_load);
268  
269 # copy because we are going to remove class, as it wasn't there to begin with.
270 63 my $config = { %{$fieldconfig} };
271 63 delete $config->{'field_class'};
272 #print Dumper($config);
273 63 return $class_to_load->new(%{$fieldconfig});
274 }
275  
276 ## clears the value for a field.
277 7 ▶sub clear_state {
Subroutine clear_state: called CC=1 SCAR=0.0
Pod documented
278 7 my $self = shift;
279  
280 7 $self->value(undef);
281 }
282  
283 16 __PACKAGE__->meta->make_immutable;
284 16 1;
285  
286 __END__
« lib/Form/Sensible/Field/SubForm.pm ↑ Summary lib/Form/Sensible/Renderer/HTML.pm »