lib/HTML/FormFu/Role/CreateChildren.pm

filter: Statementstmt 82.1% 212 / 258 Branchbran 63.8% 69 / 108 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 91.3% 21 / 23 Podpod 0.0% 0 / 8 totaltotal 76.6% 311 / 406 SCAR 48.3
CC
69
Cov
77%
_single_transformer
72.0
transformer
12.0
_single_deflator
9.6
CRAP 125.8
« lib/HTML/FormFu/Deploy.pm ↑ Summary lib/HTML/FormFu/Role/Element/SingleValueField.pm »
1 403 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Role::CreateChildren;
4 403 $HTML::FormFu::Role::CreateChildren::VERSION = '2.06';
5 403 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 403 ▶use HTML::FormFu::Util qw( _merge_hashes require_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 403 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 403 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 403 ▶use List::Util 1.45 qw( uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 403 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub element {
14 1240 ▶ my ( $self, $arg ) = @_;
Subroutine element: called CC=3 SCAR=11.0
Pod undocumented
15 1240 my @return;
16  
17 1240 ▶ if ( ref $arg eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 233 1007 1240
18 233 push @return, map { $self->_single_element($_) } @$arg;
19 }
20 else {
21 1007 push @return, $self->_single_element($arg);
22 }
23  
24 1239 ▶ return @return == 1 ? $return[0] : @return;
Branch: 1 branch
BranchTrueFalseTotal
@return == 1 ? : 1094 145 1239
25 }
26  
27 sub deflator {
28 2 ▶ my ( $self, $arg ) = @_;
Subroutine deflator: called CC=3 SCAR=12.3
Pod undocumented
29 2 my @return;
30  
31 2 ▶ if ( ref $arg eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 0 2 2
32 0 push @return, map { $self->_single_deflator($_) } @$arg;
33 }
34 else {
35 2 push @return, $self->_single_deflator($arg);
36 }
37  
38 2 ▶ return @return == 1 ? $return[0] : @return;
Branch: 1 branch
BranchTrueFalseTotal
@return == 1 ? : 0 2 2
39 }
40  
41 sub filter {
42 17 ▶ my ( $self, $arg ) = @_;
Subroutine filter: called CC=3 SCAR=11.0
Pod undocumented
43 17 my @return;
44  
45 17 ▶ if ( ref $arg eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 3 14 17
46 3 push @return, map { $self->_single_filter($_) } @$arg;
47 }
48 else {
49 14 push @return, $self->_single_filter($arg);
50 }
51  
52 17 ▶ return @return == 1 ? $return[0] : @return;
Branch: 1 branch
BranchTrueFalseTotal
@return == 1 ? : 1 16 17
53 }
54  
55 sub constraint {
56 58 ▶ my ( $self, $arg ) = @_;
Subroutine constraint: called CC=3 SCAR=11.0
Pod undocumented
57 58 my @return;
58  
59 58 ▶ if ( ref $arg eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 14 44 58
60 14 push @return, map { $self->_single_constraint($_) } @$arg;
61 }
62 else {
63 44 push @return, $self->_single_constraint($arg);
64 }
65  
66 58 ▶ return @return == 1 ? $return[0] : @return;
Branch: 1 branch
BranchTrueFalseTotal
@return == 1 ? : 13 45 58
67 }
68  
69 sub inflator {
70 5 ▶ my ( $self, $arg ) = @_;
Subroutine inflator: called CC=3 SCAR=11.6
Pod undocumented
71 5 my @return;
72  
73 5 ▶ if ( ref $arg eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 0 5 5
74 0 push @return, map { $self->_single_inflator($_) } @$arg;
75 }
76 else {
77 5 push @return, $self->_single_inflator($arg);
78 }
79  
80 5 ▶ return @return == 1 ? $return[0] : @return;
Branch: 1 branch
BranchTrueFalseTotal
@return == 1 ? : 3 2 5
81 }
82  
83 sub validator {
84 2 ▶ my ( $self, $arg ) = @_;
Subroutine validator: called CC=3 SCAR=11.6
Pod undocumented
85 2 my @return;
86  
87 2 ▶ if ( ref $arg eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 0 2 2
88 0 push @return, map { $self->_single_validator($_) } @$arg;
89 }
90 else {
91 2 push @return, $self->_single_validator($arg);
92 }
93  
94 2 ▶ return @return == 1 ? $return[0] : @return;
Branch: 1 branch
BranchTrueFalseTotal
@return == 1 ? : 1 1 2
95 }
96  
97 sub transformer {
98 0 ▶ my ( $self, $arg ) = @_;
Subroutine transformer: not called CC=3 SCAR=24.8
Pod undocumented
99 0 my @return;
100  
101 0 ▶ if ( ref $arg eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 0 0 0
102 0 push @return, map { $self->_single_transformer($_) } @$arg;
103 }
104 else {
105 0 push @return, $self->_single_transformer($arg);
106 }
107  
108 0 ▶ return @return == 1 ? $return[0] : @return;
Branch: 1 branch
BranchTrueFalseTotal
@return == 1 ? : 0 0 0
109 }
110  
111 sub plugin {
112 1 ▶ my ( $self, $arg ) = @_;
Subroutine plugin: called CC=3 SCAR=11.6
Pod undocumented
113 1 my @return;
114  
115 1 ▶ if ( ref $arg eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 1 0 1
116 1 push @return, map { $self->_single_plugin($_) } @$arg;
117 }
118 else {
119 0 push @return, $self->_single_plugin($arg);
120 }
121  
122 1 ▶ return @return == 1 ? $return[0] : @return;
Branch: 1 branch
BranchTrueFalseTotal
@return == 1 ? : 1 0 1
123 }
124  
125 sub _require_element {
126 1466 ▶ my ( $self, $arg ) = @_;
Subroutine _require_element: called CC=6 SCAR=17.9
127  
128 1466 ▶ $arg->{type} = 'Text' if !exists $arg->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg->{"type"} 224 1242 1466
129  
130 1466 my $type = delete $arg->{type};
131 1466 my $class = $type;
132  
133 1466 ▶ if ( not $class =~ s/^\+// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class =~ s/^\+//) 1465 1 1466
134 1465 $class = "HTML::FormFu::Element::$class";
135 }
136  
137 1466 $type =~ s/^\+//;
138  
139 1466 require_class($class);
140  
141 1466 my $element = $class->new(
142 { type => $type,
143 parent => $self,
144 } );
145  
146 1466 my $default_args = $self->default_args;
147  
148 1466 ▶ if (%$default_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$default_args) 265 1201 1466
149 265 ▶ if ( $element->can('default_args') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->can("default_args")) 77 188 265
150 77 $element->default_args( Clone::clone($default_args) );
151 }
152  
153 $default_args = $element->_match_default_args(
154 265 Clone::clone( $default_args->{elements} ) );
155  
156 265 ▶ if (%$default_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$default_args) 18 247 265
157 18 $arg = _merge_hashes( $arg, $default_args );
158 }
159 }
160  
161 1466 $element->populate($arg);
162  
163 1465 $element->setup;
164  
165 1465 return $element;
166 }
167  
168 sub _single_element {
169 1466 ▶ my ( $self, $arg ) = @_;
Subroutine _single_element: called CC=6 SCAR=17.9
170  
171 1466 ▶ if ( !ref $arg ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $arg) { } 679 787 1466
elsif (ref $arg eq 'HASH') { } 787 0 787
172 679 $arg = { type => $arg };
173 }
174 elsif ( ref $arg eq 'HASH' ) {
175 787 $arg = {%$arg}; # shallow clone
176 }
177 else {
178 0 croak 'invalid args';
179 }
180  
181 1466 my $new = $self->_require_element($arg);
182  
183 1465 ▶ if ( $self->can('auto_fieldset')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('auto_fieldset') and $self->auto_fieldset and $new->type ne 'Fieldset') { } 200 1265 1465
Condition100%6/6
$self->can("auto_fieldset") and $self->auto_fieldset and $new->type ne "Fieldset"
!ll&&!rl&&r
12623200
$self->can("auto_fieldset") and $self->auto_fieldset
!ll&&!rl&&r
553709203
MC/DC100%3/3
$self->can("auto_fieldset") and $self->auto_fieldset and $new->type ne "Fieldset"
$self->can("auto_fieldset") $self->auto_fieldset $new->type ne "Fieldset"
Truth table100%4/4
A and B and C
A= $self->can("auto_fieldset")B= $self->auto_fieldsetC= $new->type ne "Fieldset"
ABCresult
0XX0
10X0
1100
1111
184 && $self->auto_fieldset
185 && $new->type ne 'Fieldset' )
186 {
187 my ($target)
188 200 = reverse @{ $self->get_elements( { type => 'Fieldset' } ) };
189  
190 200 push @{ $target->_elements }, $new;
191  
192 200 $new->{parent} = $target;
193 200 weaken $new->{parent};
194 }
195 else {
196 1265 push @{ $self->_elements }, $new;
197 }
198  
199 1465 return $new;
200 }
201  
202 sub _single_deflator {
203 2 ▶ my ( $self, $arg ) = @_;
Subroutine _single_deflator: called CC=8 SCAR=22.6
204  
205 2 ▶ if ( !ref $arg ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $arg) { } 2 0 2
elsif (ref $arg eq 'HASH') { } 0 0 0
206 2 $arg = { type => $arg };
207 }
208 elsif ( ref $arg eq 'HASH' ) {
209 0 $arg = {%$arg}; # shallow clone
210 }
211 else {
212 0 croak 'invalid args';
213 }
214  
215 0 ▶ my @names = map { ref $_ ? @$_ : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
216 2 grep {defined} ( delete $arg->{name}, delete $arg->{names} );
217  
218 2 ▶ if ( !@names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 2 0 2
219 @names = uniq
220 4 grep {defined}
221 2 map { $_->nested_name } @{ $self->get_fields };
222 }
223  
224 2 ▶ croak "no field names to add deflator to" if !@names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 2 2
225  
226 2 my $type = delete $arg->{type};
227  
228 2 my @return;
229  
230 2 for my $x (@names) {
231 4 for my $field ( @{ $self->get_fields( { nested_name => $x } ) } ) {
232 4 my $new = $field->_require_deflator( $type, $arg );
233 4 push @{ $field->_deflators }, $new;
234 4 push @return, $new;
235 }
236 }
237  
238 2 return @return;
239 }
240  
241 sub _single_filter {
242 17 ▶ my ( $self, $arg ) = @_;
Subroutine _single_filter: called CC=8 SCAR=20.9
243  
244 17 ▶ if ( !ref $arg ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $arg) { } 15 2 17
elsif (ref $arg eq 'HASH') { } 2 0 2
245 15 $arg = { type => $arg };
246 }
247 elsif ( ref $arg eq 'HASH' ) {
248 2 $arg = {%$arg}; # shallow clone
249 }
250 else {
251 0 croak 'invalid args';
252 }
253  
254 2 ▶ my @names = map { ref $_ ? @$_ : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 2 0 2
255 17 grep {defined} ( delete $arg->{name}, delete $arg->{names} );
256  
257 17 ▶ if ( !@names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 15 2 17
258 @names = uniq
259 37 grep {defined}
260 15 map { $_->nested_name } @{ $self->get_fields };
261 }
262  
263 17 ▶ croak "no field names to add filter to" if !@names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 17 17
264  
265 17 my $type = delete $arg->{type};
266  
267 17 my @return;
268  
269 17 for my $x (@names) {
270 41 for my $field ( @{ $self->get_fields( { nested_name => $x } ) } ) {
271 41 my $new = $field->_require_filter( $type, $arg );
272 41 push @{ $field->_filters }, $new;
273 41 push @return, $new;
274 }
275 }
276  
277 17 return @return;
278 }
279  
280 sub _single_constraint {
281 59 ▶ my ( $self, $arg ) = @_;
Subroutine _single_constraint: called CC=8 SCAR=20.8
282  
283 59 ▶ if ( !ref $arg ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $arg) { } 44 15 59
elsif (ref $arg eq 'HASH') { } 15 0 15
284 44 $arg = { type => $arg };
285 }
286 elsif ( ref $arg eq 'HASH' ) {
287 15 $arg = {%$arg}; # shallow clone
288 }
289 else {
290 0 croak 'invalid args';
291 }
292  
293 11 ▶ my @names = map { ref $_ ? @$_ : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 6 5 11
294 59 grep {defined} ( delete $arg->{name}, delete $arg->{names} );
295  
296 59 ▶ if ( !@names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 48 11 59
297 @names = uniq
298 96 grep {defined}
299 48 map { $_->nested_name } @{ $self->get_fields };
300 }
301  
302 59 ▶ croak "no field names to add constraint to" if !@names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 59 59
303  
304 59 my $type = delete $arg->{type};
305  
306 59 my @return;
307  
308 59 for my $x (@names) {
309 112 for my $field ( @{ $self->get_fields( { nested_name => $x } ) } ) {
310 113 my $new = $field->_require_constraint( $type, $arg );
311 113 push @{ $field->_constraints }, $new;
312 113 push @return, $new;
313 }
314 }
315  
316 59 return @return;
317 }
318  
319 sub _single_inflator {
320 5 ▶ my ( $self, $arg ) = @_;
Subroutine _single_inflator: called CC=8 SCAR=20.9
321  
322 5 ▶ if ( !ref $arg ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $arg) { } 4 1 5
elsif (ref $arg eq 'HASH') { } 1 0 1
323 4 $arg = { type => $arg };
324 }
325 elsif ( ref $arg eq 'HASH' ) {
326 1 $arg = {%$arg}; # shallow clone
327 }
328 else {
329 0 croak 'invalid args';
330 }
331  
332 1 ▶ my @names = map { ref $_ ? @$_ : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 1 1
333 5 grep {defined} ( delete $arg->{name}, delete $arg->{names} );
334  
335 5 ▶ if ( !@names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 4 1 5
336 @names = uniq
337 6 grep {defined}
338 4 map { $_->nested_name } @{ $self->get_fields };
339 }
340  
341 5 ▶ croak "no field names to add inflator to" if !@names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 5 5
342  
343 5 my $type = delete $arg->{type};
344  
345 5 my @return;
346  
347 5 for my $x (@names) {
348 7 for my $field ( @{ $self->get_fields( { nested_name => $x } ) } ) {
349 7 my $new = $field->_require_inflator( $type, $arg );
350 7 push @{ $field->_inflators }, $new;
351 7 push @return, $new;
352 }
353 }
354  
355 5 return @return;
356 }
357  
358 sub _single_validator {
359 2 ▶ my ( $self, $arg ) = @_;
Subroutine _single_validator: called CC=8 SCAR=20.9
360  
361 2 ▶ if ( !ref $arg ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $arg) { } 1 1 2
elsif (ref $arg eq 'HASH') { } 1 0 1
362 1 $arg = { type => $arg };
363 }
364 elsif ( ref $arg eq 'HASH' ) {
365 1 $arg = {%$arg}; # shallow clone
366 }
367 else {
368 0 croak 'invalid args';
369 }
370  
371 1 ▶ my @names = map { ref $_ ? @$_ : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 1 1
372 2 grep {defined} ( delete $arg->{name}, delete $arg->{names} );
373  
374 2 ▶ if ( !@names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 1 1 2
375 @names = uniq
376 2 grep {defined}
377 1 map { $_->nested_name } @{ $self->get_fields };
378 }
379  
380 2 ▶ croak "no field names to add validator to" if !@names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 2 2
381  
382 2 my $type = delete $arg->{type};
383  
384 2 my @return;
385  
386 2 for my $x (@names) {
387 3 for my $field ( @{ $self->get_fields( { nested_name => $x } ) } ) {
388 3 my $new = $field->_require_validator( $type, $arg );
389 3 push @{ $field->_validators }, $new;
390 3 push @return, $new;
391 }
392 }
393  
394 2 return @return;
395 }
396  
397 sub _single_transformer {
398 0 ▶ my ( $self, $arg ) = @_;
Subroutine _single_transformer: not called CC=8 SCAR=42.8
399  
400 0 ▶ if ( !ref $arg ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $arg) { } 0 0 0
elsif (ref $arg eq 'HASH') { } 0 0 0
401 0 $arg = { type => $arg };
402 }
403 elsif ( ref $arg eq 'HASH' ) {
404 0 $arg = {%$arg}; # shallow clone
405 }
406 else {
407 0 croak 'invalid args';
408 }
409  
410 0 ▶ my @names = map { ref $_ ? @$_ : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
411 0 grep {defined} ( delete $arg->{name}, delete $arg->{names} );
412  
413 0 ▶ if ( !@names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 0 0 0
414 @names = uniq
415 0 grep {defined}
416 0 map { $_->nested_name } @{ $self->get_fields };
417 }
418  
419 0 ▶ croak "no field names to add transformer to" if !@names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 0 0
420  
421 0 my $type = delete $arg->{type};
422  
423 0 my @return;
424  
425 0 for my $x (@names) {
426 0 for my $field ( @{ $self->get_fields( { nested_name => $x } ) } ) {
427 0 my $new = $field->_require_transformer( $type, $arg );
428 0 push @{ $field->_transformers }, $new;
429 0 push @return, $new;
430 }
431 }
432  
433 0 return @return;
434 }
435  
436 403 1;
437  
438 __END__
439  
440 =pod
441  
442 =encoding UTF-8
443  
444 =head1 NAME
445  
446 HTML::FormFu::Role::CreateChildren
447  
448 =head1 VERSION
449  
450 version 2.06
451  
452 =head1 AUTHOR
453  
454 Carl Franks <cpan@fireartist.com>
455  
456 =head1 COPYRIGHT AND LICENSE
457  
458 This software is copyright (c) 2018 by Carl Franks.
459  
460 This is free software; you can redistribute it and/or modify it under
461 the same terms as the Perl 5 programming language system itself.
462  
463 =cut
« lib/HTML/FormFu/Deploy.pm ↑ Summary lib/HTML/FormFu/Role/Element/SingleValueField.pm »