lib/HTML/FormFu/Element/Repeatable.pm

filter: Statementstmt 99.4% 175 / 176 Branchbran 81.2% 39 / 48 Conditioncond 70.0% 14 / 20 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 18 / 18 Podpod 40.0% 2 / 5 totaltotal 90.6% 253 / 279 CC 41 SCAR 19.6
CC
41
Cov
93%
process
12.1
_repeat_containing_block
10.0
_repeat_child_elements
9.0
CRAP 7.1
« lib/HTML/FormFu/Inflator/CompoundDateTime.pm ↑ Summary lib/HTML/FormFu/Role/Element/Layout.pm »
1 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Element::Repeatable;
4 38 $HTML::FormFu::Element::Repeatable::VERSION = '2.09';
5 # ABSTRACT: repeatable block element
6  
7 38 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 38 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 38 extends 'HTML::FormFu::Element::Block';
10  
11 38 ▶use HTML::FormFu::Util qw( DEBUG_PROCESS debug );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 38 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 38 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 38 has counter_name => ( is => 'rw', traits => ['Chained'] );
16  
17 has max_counter => (
18 is => 'rw',
19 28 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
20 28 my $form = $_[0]->form;
21 28 ▶ return $form->repeatable_max_counter if $form;
Branch: 1 branch
BranchTrueFalseTotal
if $form 28 0 28
22 0 return 100;
23 },
24 38 lazy => 1,
25 traits => ['Chained'],
26 );
27  
28 38 has counter_clamped => (
29 is => 'ro',
30 default => 0,
31 writer => '_set_counter_clamped',
32 );
33  
34 38 has _original_elements => ( is => 'rw' );
35  
36 38 has increment_field_names => (
37 is => 'rw',
38 default => 1,
39 lazy => 1,
40 traits => ['Chained'],
41 );
42  
43 # This attribute is currently not documented as FF::Model::HashRef
44 # only supports '_'
45  
46 38 has repeatable_delimiter => (
47 is => 'rw',
48 default => '_',
49 lazy => 1,
50 traits => ['Chained'],
51 );
52  
53 44 ▶after BUILD => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 44 my $self = shift;
55  
56 44 $self->filename('repeatable');
57 44 $self->is_repeatable(1);
58  
59 44 return;
60 38 };
61  
62 53 ▶sub repeat {
Subroutine repeat: called CC=7 SCAR=19.6
Pod documented
63 53 my ( $self, $count ) = @_;
64  
65 53 ▶ croak "invalid number to repeat"
Branch: 1 branch
BranchTrueFalseTotal
unless $count =~ /^[0-9]+\z/ 0 53 53
66 if $count !~ /^[0-9]+\z/;
67  
68 53 my $children;
69  
70 53 ▶ if ( $self->_original_elements ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_original_elements) { } 6 47 53
71  
72 # repeat() has already been called
73 6 $children = $self->_original_elements;
74 }
75 else {
76 47 $children = $self->_elements;
77  
78 47 $self->_original_elements($children);
79 }
80  
81 53 ▶ croak "no child elements to repeat"
Branch: 1 branch
BranchTrueFalseTotal
unless @$children 0 53 53
82 if !@$children;
83  
84 53 $self->_elements( [] );
85  
86 53 ▶ return [] if !$count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 1 52 53
87  
88 # switch behaviour
89 # If nested_name is set, we add the repeatable counter to the name
90 # of the containing block (this repeatable block).
91 # This behaviour eases the creation of client side javascript code
92 # to add and remove repeatable elements client side.
93 # If nested_name is *not* set, we add the repeatable counter to the names
94 # of the child elements (leaves of the element tree).
95 52 my $nested_name = $self->nested_name;
96 52 ▶ if ( defined $nested_name && length $nested_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nested_name and length $nested_name) { } 25 27 52
Condition66%2/3
defined $nested_name and length $nested_name
!ll&&!rl&&r
27025
MC/DC50%1/2
defined $nested_name and length $nested_name
defined $nested_name length $nested_name
Truth table66%2/3
A and B
A= defined $nested_nameB= length $nested_name
ABresult
0X0
100
111
97 25 return $self->_repeat_containing_block($count);
98 }
99 else {
100 27 return $self->_repeat_child_elements($count);
101 }
102 }
103  
104 25 ▶sub _repeat_containing_block {
Subroutine _repeat_containing_block: called CC=10 SCAR=23.0
105 25 my ( $self, $count ) = @_;
106  
107 25 my $children = $self->_original_elements;
108  
109 # We must not get 'nested.nested_1' instead of 'nested_1' through the
110 # nested_name attribute of the Repeatable element, thus we extended
111 # FF::Elements::_Field nested_names method to ignore Repeatable elements.
112 25 my $nested_name = $self->nested_name;
113 25 $self->original_nested_name($nested_name);
114  
115 # delimiter between nested_name and the incremented counter
116 25 my $delimiter = $self->repeatable_delimiter;
117  
118 25 my @return;
119  
120 25 for my $rep ( 1 .. $count ) {
121  
122 # create clones of elements and put them in a new block
123 397 my @clones = map { $_->clone } @$children;
124 397 my $block = $self->element('Block');
125  
126 # initiate new block with properties of this repeatable
127 397 $block->_elements( \@clones );
128 397 $block->attributes( $self->attributes );
129 397 $block->tag( $self->tag );
130  
131 397 $block->repeatable_count($rep);
132  
133 397 ▶ if ( $self->increment_field_names ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->increment_field_names) 397 0 397
134  
135 # store the original nested_name attribute for later usage when
136 # building the original nested name
137 397 ▶ $block->original_nested_name( $block->nested_name )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $block->original_nested_name 397 0 397
138 if !defined $block->original_nested_name;
139  
140 # create new nested name with repeat counter
141 397 $block->nested_name( $nested_name . $delimiter . $rep );
142  
143 397 for my $field ( @{ $block->get_fields } ) {
144  
145 439 ▶ if ( defined( my $name = $field->name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $name = $field->name)) 439 0 439
146  
147 # store original name for later usage when
148 # replacing the field names in constraints
149 439 ▶ $field->original_name($name)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field->original_name 432 7 439
150 if !defined $field->original_name;
151  
152 # store original nested name for later usage when
153 # replacing the field names in constraints
154 439 ▶ $field->original_nested_name(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field->original_nested_name 432 7 439
155 $field->build_original_nested_name )
156 if !defined $field->original_nested_name;
157 }
158 }
159 }
160  
161 397 _reparent_children($block);
162  
163 397 my @fields = @{ $block->get_fields };
164  
165 397 for my $field (@fields) {
166 19 map { $_->parent($field) }
167 439 @{ $field->_deflators },
168 439 @{ $field->_filters },
169 439 @{ $field->_constraints },
170 439 @{ $field->_inflators },
171 439 @{ $field->_validators },
172 439 @{ $field->_transformers },
173 439 @{ $field->_plugins },
174 ;
175 }
176  
177 397 for my $field (@fields) {
178 13 map { $_->repeatable_repeat( $self, $block ) }
179 439 @{ $field->_constraints };
180 }
181  
182 397 push @return, $block;
183 }
184  
185 25 return \@return;
186 }
187  
188 11 ▶sub get_field_with_original_name {
Subroutine get_field_with_original_name: called CC=2 SCAR=6.9
Pod undocumented
189 11 my ( $self, $name, $fields ) = @_;
190  
191 23 ▶ my $field = first { $_->original_nested_name eq $name }
Subroutine __ANON__: called CC=1 SCAR=0.0
192 11 grep { defined $_->original_nested_name } @$fields;
193  
194 4 ▶ $field ||= first { $_->original_name eq $name }
Subroutine __ANON__: called CC=1 SCAR=0.0
195 11 ▶ grep { defined $_->original_name } @$fields;
Condition66%2/3
$field ||= &first(sub { $_->original_name eq $name; } , grep({defined $_->original_name;} @$fields))
l!l&&r!l&&!r
920
MC/DC0%0/2
$field ||= &first(sub { $_->original_name eq $name; } , grep({defined $_->original_name;} @$fields))
$field &first(sub { $_->original_name eq $name; } , grep({defined $_->original_name;} @$fields))
Truth table66%2/3
A ||= B
A= $fieldB= &first(sub { $_->original_name eq $name; } , grep({defined $_->original_name;} @$fields))
ABresult
1X1
011
000
196  
197 11 return $field;
198 }
199  
200 27 ▶sub _repeat_child_elements {
Subroutine _repeat_child_elements: called CC=9 SCAR=22.0
201 27 my ( $self, $count ) = @_;
202  
203 27 my $children = $self->_original_elements;
204  
205 # delimiter between nested_name and the incremented counter
206 27 my $delimiter = $self->repeatable_delimiter;
207  
208 27 my @return;
209  
210 27 for my $rep ( 1 .. $count ) {
211 51 my @clones = map { $_->clone } @$children;
212 51 my $block = $self->element('Block');
213  
214 51 $block->_elements( \@clones );
215 51 $block->attributes( $self->attributes );
216 51 $block->tag( $self->tag );
217  
218 51 $block->repeatable_count($rep);
219  
220 51 ▶ if ( $self->increment_field_names ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->increment_field_names) 42 9 51
221 42 for my $field ( @{ $block->get_fields } ) {
222  
223 83 ▶ if ( defined( my $name = $field->name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $name = $field->name)) 83 0 83
224 83 ▶ $field->original_name($name)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field->original_name 71 12 83
225 if !defined $field->original_name;
226  
227 83 ▶ $field->original_nested_name( $field->nested_name )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field->original_nested_name 71 12 83
228 if !defined $field->original_nested_name;
229  
230 83 $field->name( ${name} . $delimiter . $rep );
231 }
232 }
233 }
234  
235 51 _reparent_children($block);
236  
237 51 my @fields = @{ $block->get_fields };
238  
239 51 for my $field (@fields) {
240 39 map { $_->parent($field) }
241 96 @{ $field->_deflators },
242 96 @{ $field->_filters },
243 96 @{ $field->_constraints },
244 96 @{ $field->_inflators },
245 96 @{ $field->_validators },
246 96 @{ $field->_transformers },
247 96 @{ $field->_plugins },
248 ;
249 }
250  
251 51 for my $field (@fields) {
252 36 map { $_->repeatable_repeat( $self, $block ) }
253 96 @{ $field->_constraints };
254 }
255  
256 51 push @return, $block;
257 }
258  
259 27 return \@return;
260 }
261  
262 999 ▶sub _reparent_children {
Subroutine _reparent_children: called CC=3 SCAR=11.0
263 999 my $self = shift;
264  
265 999 ▶ return if !$self->is_block;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_block 526 473 999
266  
267 473 for my $child ( @{ $self->get_elements } ) {
268 551 $child->parent($self);
269  
270 551 _reparent_children($child);
271 }
272 }
273  
274 59 ▶sub process {
Subroutine process: called CC=12 SCAR=24.9
Pod undocumented
275 59 my $self = shift;
276  
277 59 my $counter_name = $self->counter_name;
278 59 my $form = $self->form;
279 59 my $count = 1;
280  
281 59 $self->_set_counter_clamped(0);
282  
283 59 ▶ if ( defined $counter_name && defined $form->query ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $counter_name and defined $form->query) 49 10 59
Condition66%2/3
defined $counter_name and defined $form->query
!ll&&!rl&&r
10049
MC/DC50%1/2
defined $counter_name and defined $form->query
defined $counter_name defined $form->query
Truth table66%2/3
A and B
A= defined $counter_nameB= defined $form->query
ABresult
0X0
100
111
284  
285 # are we in a nested-repeatable?
286 49 my $parent = $self;
287  
288 49 ▶ while ( defined( $parent = $parent->parent ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($parent = $parent->parent)) 54 39 93
289 54 my $field
290 = $parent->get_field( { original_name => $counter_name } );
291  
292 54 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) 10 44 54
293 10 $counter_name = $field->nested_name;
294 10 last;
295 }
296 }
297  
298 49 my $input = $form->query->param($counter_name);
299  
300 49 ▶ if ( defined $input && $input =~ /^[1-9][0-9]*\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input and $input =~ /^[1-9][0-9]*\z/) 48 1 49
Condition66%2/3
defined $input and $input =~ /^[1-9][0-9]*\z/
!ll&&!rl&&r
1048
MC/DC50%1/2
defined $input and $input =~ /^[1-9][0-9]*\z/
defined $input $input =~ /^[1-9][0-9]*\z/
Truth table66%2/3
A and B
A= defined $inputB= $input =~ /^[1-9][0-9]*\z/
ABresult
0X0
100
111
301 48 my $max = $self->max_counter;
302  
303 48 ▶ if ( defined $max && $max > 0 && $input > $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max and $max > 0 and $input > $max) { } 3 45 48
Condition83%5/6
defined $max and $max > 0 and $input > $max
!ll&&!rl&&r
1443
defined $max and $max > 0
!ll&&!rl&&r
0147
MC/DC66%2/3
defined $max and $max > 0 and $input > $max
defined $max $max > 0 $input > $max
Truth table75%3/4
A and B and C
A= defined $maxB= $max > 0C= $input > $max
ABCresult
0XX0
10X0
1100
1111
304 3 $count = $max;
305 3 $self->_set_counter_clamped(1);
306 }
307 else {
308 45 $count = $input;
309 }
310 }
311 }
312  
313 59 ▶ if ( !$self->_original_elements ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_original_elements) 25 34 59
314 25 ▶ DEBUG_PROCESS && debug("calling \$repeatable->repeat($count)");
Branch: 1 branch
BranchTrueFalseTotal
&DEBUG_PROCESS() and debug("calling \$repeatable->repeat($count)") 0 25 25
315  
316 25 $self->repeat($count);
317 }
318  
319 59 return $self->SUPER::process(@_);
320 }
321  
322 7 ▶sub content {
Subroutine content: called CC=2 SCAR=7.1
Pod documented
323 7 my $self = shift;
324  
325 7 ▶ croak "Repeatable elements do not support the content() method"
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 7 7
326 if @_;
327  
328 7 return;
329 }
330  
331 22 ▶sub string {
Subroutine string: called CC=2 SCAR=7.0
Pod undocumented
332 22 my ( $self, $args ) = @_;
333  
334 22 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
022
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
335  
336  
337 # block template
338  
339 22 my @divs = map { $_->render } @{ $self->get_elements };
340  
341 22 my $html = join "\n", @divs;
342  
343 22 return $html;
344 }
345  
346 38 __PACKAGE__->meta->make_immutable;
347  
348 38 1;
349  
350 __END__
« lib/HTML/FormFu/Inflator/CompoundDateTime.pm ↑ Summary lib/HTML/FormFu/Role/Element/Layout.pm »