FormHandler/Field/Repeatable.pm

filter: Statementstmt 99.4% 185 / 186 Branchbran 80.6% 50 / 62 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 18 / 18 Podpod 20.0% 1 / 5 totaltotal 90.0% 262 / 291 CC 42 SCAR 18.4
CC
42
Cov
93%
_result_from_object
12.5
create_element
10.2
_result_from_input
5.0
CRAP 6.3
« FormHandler/Widget/Wrapper/Table.pm ↑ Summary FormHandler/Widget/Wrapper/Base.pm »
1 package HTML::FormHandler::Field::Repeatable;
2 # ABSTRACT: repeatable (array) field
3 29 $HTML::FormHandler::Field::Repeatable::VERSION = '0.410002';
4 29 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 extends 'HTML::FormHandler::Field::Compound';
6  
7 29 ▶use HTML::FormHandler::Field::Repeatable::Instance;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use HTML::FormHandler::Field::PrimaryKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use HTML::FormHandler::Merge ('merge');
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use Data::Clone ('data_clone');
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 29 has 'contains' => (
14 isa => 'HTML::FormHandler::Field',
15 is => 'rw',
16 predicate => 'has_contains',
17 );
18  
19 has 'init_contains' => ( is => 'rw', isa => 'HashRef', traits => ['Hash'],
20 45 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
21 29 handles => { has_init_contains => 'count' },
22 );
23  
24 29 has 'num_when_empty' => ( isa => 'Int', is => 'rw', default => 1 );
25 29 has 'num_extra' => ( isa => 'Int', is => 'rw', default => 0 );
26 29 has 'setup_for_js' => ( isa => 'Bool', is => 'rw' );
27 29 has 'index' => ( isa => 'Int', is => 'rw', default => 0 );
28 29 has 'auto_id' => ( isa => 'Bool', is => 'rw', default => 0 );
29 29 has 'is_repeatable' => ( isa => 'Bool', is => 'ro', default => 1 );
30 29 has '+widget' => ( default => 'Repeatable' );
31  
32 41 ▶sub _fields_validate {
Subroutine _fields_validate: called CC=4 SCAR=13.9
33 41 my $self = shift;
34 # loop through array of fields and validate
35 41 my @value_array;
36 41 foreach my $field ( $self->all_fields ) {
37 80 ▶ next if ( $field->is_inactive );
Branch: 1 branch
BranchTrueFalseTotal
if $field->is_inactive 0 80 80
38 # Validate each field and "inflate" input -> value.
39 80 $field->validate_field; # this calls the field's 'validate' routine
40 80 ▶ push @value_array, $field->value if $field->has_value;
Branch: 1 branch
BranchTrueFalseTotal
if $field->has_value 77 3 80
41 }
42 41 $self->_set_value( \@value_array );
43 }
44  
45 120 ▶sub init_state {
Subroutine init_state: called CC=4 SCAR=13.9
Pod undocumented
46 120 my $self = shift;
47  
48 # must clear out instances built last time
49 120 ▶ unless ( $self->has_contains ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_contains) 49 71 120
50 49 ▶ if ( $self->num_fields == 1 && $self->field('contains') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num_fields == 1 and $self->field('contains')) { } 16 33 49
Condition100%3/3
$self->num_fields == 1 and $self->field("contains")
!ll&&!rl&&r
31216
MC/DC100%2/2
$self->num_fields == 1 and $self->field("contains")
$self->num_fields == 1 $self->field("contains")
Truth table100%3/3
A and B
A= $self->num_fields == 1B= $self->field("contains")
ABresult
0X0
100
111
51 16 $self->field('contains')->is_contains(1);
52 16 $self->contains( $self->field('contains') );
53 }
54 else {
55 33 $self->contains( $self->create_element );
56 }
57 }
58 120 $self->clear_fields;
59 }
60  
61 33 ▶sub create_element {
Subroutine create_element: called CC=10 SCAR=23.2
Pod undocumented
62 33 my ($self) = @_;
63  
64 33 my $instance;
65 33 my $instance_attr = {
66 name => 'contains',
67 parent => $self,
68 type => 'Repeatable::Instance',
69 is_contains => 1,
70 };
71 # primary_key array is used for reloading after database update
72 33 ▶ $instance_attr->{primary_key} = $self->primary_key
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_primary_key 7 26 33
73 if $self->has_primary_key;
74 33 ▶ if( $self->has_init_contains ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_init_contains) 4 29 33
75 4 $instance_attr = merge( $self->init_contains, $instance_attr );
76 }
77 33 ▶ if( $self->form ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form) { } 32 1 33
78 32 $instance_attr->{form} = $self->form;
79 32 $instance = $self->form->_make_adhoc_field(
80 'HTML::FormHandler::Field::Repeatable::Instance',
81 $instance_attr );
82 }
83 else {
84 1 $instance = HTML::FormHandler::Field::Repeatable::Instance->new( %$instance_attr );
85 }
86 # copy the fields from this field into the instance
87 33 $instance->add_field( $self->all_fields );
88 33 foreach my $fld ( $instance->all_fields ) {
89 88 $fld->parent($instance);
90 }
91  
92 # set required flag
93 33 $instance->required( $self->required );
94  
95 # auto_id has no way to change widgets...deprecate this?
96 33 ▶ if ( $self->auto_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_id) 4 29 33
97 4 ▶ unless ( grep $_->can('is_primary_key') && $_->is_primary_key, $instance->all_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep(($_->can("is_primary_key") && $_->is_primary_key), $instance->all_fields)) 4 0 4
Condition33%1/3
$_->can('is_primary_key') && $_->is_primary_key
!ll&&!rl&&r
1500
MC/DC0%0/2
$_->can('is_primary_key') && $_->is_primary_key
$_->can('is_primary_key') $_->is_primary_key
Truth table33%1/3
A && B
A= $_->can('is_primary_key')B= $_->is_primary_key
ABresult
0X0
100
111
98 4 my $field;
99 4 my $field_attr = { name => 'id', parent => $instance };
100 4 ▶ if ( $self->form ) { # this will pull in the widget role
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form) { } 4 0 4
101 4 $field_attr->{form} = $self->form;
102 4 $field = $self->form->_make_adhoc_field(
103 'HTML::FormHandler::Field::PrimaryKey', $field_attr );
104 }
105 else { # the following won't have a widget role applied
106 0 $field = HTML::FormHandler::Field::PrimaryKey->new( %$field_attr );
107 }
108 4 $instance->add_field($field);
109 }
110 }
111 33 $_->parent($instance) for $instance->all_fields;
112 33 return $instance;
113 }
114  
115 195 ▶sub clone_element {
Subroutine clone_element: called CC=2 SCAR=6.9
Pod undocumented
116 195 my ( $self, $index ) = @_;
117  
118 195 my $field = $self->contains->clone( errors => [], error_fields => [] );
119 195 $field->name($index);
120 195 $field->parent($self);
121 195 ▶ if ( $field->has_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->has_fields) 142 53 195
122 142 $self->clone_fields( $field, [ $field->all_fields ] );
123 }
124 195 return $field;
125 }
126  
127 152 ▶sub clone_fields {
Subroutine clone_fields: called CC=3 SCAR=11.0
Pod undocumented
128 152 my ( $self, $parent, $fields ) = @_;
129 152 my @field_array;
130 152 $parent->fields( [] );
131 152 foreach my $field ( @{$fields} ) {
132 462 my $new_field = $field->clone( errors => [], error_fields => [] );
133 462 ▶ if ( $new_field->has_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_field->has_fields) 10 452 462
134 10 $self->clone_fields( $new_field, [ $new_field->all_fields ] );
135 }
136 462 $new_field->parent($parent);
137 462 $parent->add_field($new_field);
138 }
139 }
140  
141 # params exist and validation will be performed (later)
142 42 ▶sub _result_from_input {
Subroutine _result_from_input: called CC=5 SCAR=16.1
143 42 my ( $self, $result, $input ) = @_;
144  
145 42 $self->init_state;
146 42 $result->_set_input($input);
147 42 $self->_set_result($result);
148 # if Repeatable has array input, need to build instances
149 42 $self->fields( [] );
150 42 my $index = 0;
151 42 ▶ if ( ref $input eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input eq "ARRAY") 39 3 42
152 # build appropriate instance array
153 39 foreach my $element ( @{$input} ) {
154 84 ▶ next if not defined $element; # skip empty slots
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element 2 82 84
155 82 my $field = $self->clone_element($index);
156 82 my $result = HTML::FormHandler::Field::Result->new(
157 name => $index,
158 parent => $self->result
159 );
160 82 $result = $field->_result_from_input( $result, $element, 1 );
161 82 $self->result->add_result($result);
162 82 $self->add_field($field);
163 82 $index++;
164 }
165 }
166 42 $self->index($index);
167 42 ▶ $self->_setup_for_js if $self->setup_for_js;
Branch: 1 branch
BranchTrueFalseTotal
if $self->setup_for_js 0 42 42
168 42 $self->result->_set_field_def($self);
169 42 return $self->result;
170 }
171  
172 1 ▶sub _setup_for_js {
Subroutine _setup_for_js: called CC=2 SCAR=6.9
173 1 my $self = shift;
174 1 ▶ return unless $self->form;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->form 0 1 1
175 1 my $full_name = $self->full_name;
176 1 my $index_level =()= $full_name =~ /{index\d+}/g;
177 1 $index_level++;
178 1 my $field_name = "{index-$index_level}";
179 1 my $field = $self->_add_extra($field_name);
180 1 my $rendered = $field->render;
181 # remove extra result & field, now that it's rendered
182 1 $self->result->_pop_result;
183 1 $self->_pop_field;
184 # set the information in the form
185 # $self->index is the index of the next instance
186 1 $self->form->set_for_js( $self->full_name,
187 { index => $self->index, html => $rendered, level => $index_level } );
188 }
189  
190 # this is called when there is an init_object or a db item with values
191 16 ▶sub _result_from_object {
Subroutine _result_from_object: called CC=12 SCAR=25.2
192 16 my ( $self, $result, $values ) = @_;
193  
194 16 ▶ return $self->_result_from_fields($result)
Branch: 1 branch
BranchTrueFalseTotal
if $self->num_when_empty > 0 and not $values 0 16 16
Condition33%1/3
$self->num_when_empty > 0 and not $values
!ll&&!rl&&r
0160
MC/DC0%0/2
$self->num_when_empty > 0 and not $values
$self->num_when_empty > 0 not $values
Truth table33%1/3
A and not(B)
A= $self->num_when_empty > 0B= not $values
ABresult
0X0
100
111
195 if ( $self->num_when_empty > 0 && !$values );
196 16 $self->item($values);
197 16 $self->init_state;
198 16 $self->_set_result($result);
199 # Create field instances and fill with values
200 16 my $index = 0;
201 16 my @new_values;
202 16 $self->fields( [] );
203 16 ▶ $values = [$values] if ( $values && ref $values ne 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if $values and ref $values ne "ARRAY" 0 16 16
Condition33%1/3
$values and ref $values ne "ARRAY"
!ll&&!rl&&r
0160
MC/DC0%0/2
$values and ref $values ne "ARRAY"
$values ref $values ne "ARRAY"
Truth table33%1/3
A and B
A= $valuesB= ref $values ne "ARRAY"
ABresult
0X0
100
111
204 16 foreach my $element ( @{$values} ) {
205 35 ▶ next unless $element;
Branch: 1 branch
BranchTrueFalseTotal
unless $element 0 35 35
206 35 my $field = $self->clone_element($index);
207 35 my $result =
208 HTML::FormHandler::Field::Result->new( name => $index, parent => $self->result );
209 35 ▶ if( $field->has_inflate_default_method ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->has_inflate_default_method) 2 33 35
210 2 $element = $field->inflate_default($element);
211 }
212 35 $result = $field->_result_from_object( $result, $element );
213 35 push @new_values, $result->value;
214 35 $self->add_field($field);
215 35 $self->result->add_result( $field->result );
216 35 $index++;
217 }
218 16 ▶ if( my $num_extra = $self->num_extra ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num_extra = $self->num_extra) 1 15 16
219 1 ▶ while ($num_extra ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_extra) 1 1 2
220 1 $self->_add_extra($index);
221 1 $num_extra--;
222 1 $index++;
223 }
224 }
225 16 $self->index($index);
226 16 ▶ $self->_setup_for_js if $self->setup_for_js;
Branch: 1 branch
BranchTrueFalseTotal
if $self->setup_for_js 0 16 16
227 16 ▶ $values = \@new_values if scalar @new_values;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @new_values 14 2 16
228 16 $self->_set_value($values);
229 16 $self->result->_set_field_def($self);
230 16 return $self->result;
231 }
232  
233 3 ▶sub _add_extra {
Subroutine _add_extra: called CC=2 SCAR=7.0
234 3 my ($self, $index) = @_;
235  
236 3 my $field = $self->clone_element($index);
237 3 my $result =
238 HTML::FormHandler::Field::Result->new( name => $index, parent => $self->result );
239 3 $result = $field->_result_from_fields($result);
240 3 ▶ $self->result->add_result($result) if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 3 0 3
241 3 $self->add_field($field);
242 3 return $field;
243 }
244  
245 1 ▶sub add_extra {
Subroutine add_extra: called CC=3 SCAR=11.0
Pod documented
246 1 my ( $self, $count ) = @_;
247 1 ▶ $count = 1 if not defined $count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 1 0 1
248 1 my $index = $self->index;
249 1 ▶ while ( $count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 1 1 2
250 1 $self->_add_extra($index);
251 1 $count--;
252 1 $index++;
253 }
254 1 $self->index($index);
255 }
256  
257 # create an empty field
258 62 ▶sub _result_from_fields {
Subroutine _result_from_fields: called CC=5 SCAR=16.1
259 62 my ( $self, $result ) = @_;
260  
261 # check for defaults
262 62 ▶ if ( my @values = $self->get_default_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@values) = $self->get_default_value) 2 60 62
263 2 return $self->_result_from_object( $result, \@values );
264 }
265 60 $self->init_state;
266 60 $self->_set_result($result);
267 60 my $count = $self->num_when_empty;
268 60 my $index = 0;
269 # build empty instance
270 60 $self->fields( [] );
271 60 ▶ while ( $count > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 75 60 135
272 75 my $field = $self->clone_element($index);
273 75 my $result =
274 HTML::FormHandler::Field::Result->new( name => $index, parent => $self->result );
275 75 $result = $field->_result_from_fields($result);
276 75 ▶ $self->result->add_result($result) if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 75 0 75
277 75 $self->add_field($field);
278 75 $index++;
279 75 $count--;
280 }
281 60 $self->index($index);
282 60 ▶ $self->_setup_for_js if $self->setup_for_js;
Branch: 1 branch
BranchTrueFalseTotal
if $self->setup_for_js 1 59 60
283 60 $self->result->_set_field_def($self);
284 60 return $result;
285 }
286  
287 29 __PACKAGE__->meta->make_immutable;
288 29 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
289 29 1;
290  
291 __END__
« FormHandler/Widget/Wrapper/Table.pm ↑ Summary FormHandler/Widget/Wrapper/Base.pm »