lib/HTML/FormFu/Model/HashRef.pm

filter: Statementstmt 95.7% 182 / 190 Branchbran 90.5% 67 / 74 Conditioncond 73.8% 31 / 42 MC/DCmcdc 54.1% 13 / 24 Subroutinesub 94.1% 16 / 17 Podpod 60.0% 3 / 5 totaltotal 88.6% 312 / 352 CC 62 SCAR 25.6
CC
62
Cov
92%
_as_object_get
21.6
_default_values
16.0
_unfold_repeatable
15.3
CRAP 12.9
« lib/HTML/FormFu/Util.pm ↑ Summary lib/HTML/FormFu/Constraint/MinMaxFields.pm »
1 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Model::HashRef;
4 7 $HTML::FormFu::Model::HashRef::VERSION = '2.09';
5 # ABSTRACT: handle hashrefs
6  
7 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 extends 'HTML::FormFu::Model';
11  
12 7 ▶use Hash::Flatten;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 7 has flatten => ( is => 'rw' );
16 7 has options => ( is => 'rw' );
17  
18 7 has _repeatable => ( is => 'rw', traits => ['Chained'] );
19 7 has _multi => ( is => 'rw', traits => ['Chained'] );
20  
21 7 has deflators => (
22 is => 'rw',
23 default => 1,
24 lazy => 1,
25 traits => ['Chained'],
26 );
27  
28 7 has inflators => (
29 is => 'rw',
30 default => 1,
31 lazy => 1,
32 traits => ['Chained'],
33 );
34  
35 8 ▶sub default_values {
Subroutine default_values: called CC=1 SCAR=0.0
Pod documented
36 8 my ( $self, $data ) = @_;
37 94 map { $_->default(undef) }
38 8 ( grep { $_->is_field } @{ $self->form->get_all_elements } );
39 8 $self->_default_values( $self->form, $data );
40 8 return $self;
41 }
42  
43 51 ▶sub _default_values {
Subroutine _default_values: called CC=16 SCAR=27.7
44 51 my ( $self, $form, $data ) = @_;
45 51 my $elements = $form->get_elements;
46 51 foreach my $element ( @{$elements} ) {
47 113 ▶ my $name = $element->name || "";
Condition100%2/2
$element->name || ''
l!l
8528
MC/DC100%1/1
$element->name || ''
$element->name
Truth table100%2/2
A
A= $element->name
Aresult
11
00
48 113 ▶ my $nested_name = $element->nested_name || "";
Condition100%2/2
$element->nested_name || ''
l!l
8924
MC/DC100%1/1
$element->nested_name || ''
$element->nested_name
Truth table100%2/2
A
A= $element->nested_name
Aresult
11
00
49 113 ▶ $name =~ s/_\d+$// if ($name);
Branch: 1 branch
BranchTrueFalseTotal
if $name 85 28 113
50 113 ▶ if ( $element->is_repeatable ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($element->is_repeatable) { } 7 106 113
elsif ($element->is_block and $element->is_field) { } 14 92 106
elsif ($element->is_block) { } 28 64 92
51 7 ▶ my $value = $data->{$name} || $data->{$nested_name};
Condition66%2/3
$data->{$name} || $data->{$nested_name}
l!l&&r!l&&!r
061
MC/DC50%1/2
$data->{$name} || $data->{$nested_name}
$data->{$name} $data->{$nested_name}
Truth table66%2/3
A || B
A= $data->{$name}B= $data->{$nested_name}
ABresult
1X1
011
000
52 7 ▶ unless ($value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value) 1 6 7
53 1 $element->repeat(0);
54 0 map { $element->remove_element($_) }
55 1 @{ $element->get_elements };
56 1 next;
57 }
58 6 my $k = scalar @{$value};
59 6 $element->repeat($k);
60 6 my $childs = $element->get_elements;
61 6 ▶ for ( my $i = 0; $i < $k; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $k) 11 6 17
62 11 $self->_default_values( $childs->[$i], $value->[$i] );
63 }
64 }
65 ▶ elsif ( $element->is_block && $element->is_field )
Condition100%3/3
$element->is_block and $element->is_field
!ll&&!rl&&r
642814
MC/DC100%2/2
$element->is_block and $element->is_field
$element->is_block $element->is_field
Truth table100%3/3
A and B
A= $element->is_blockB= $element->is_field
ABresult
0X0
100
111
66 { # is a Multi element
67 ref $data->{$name} eq "HASH"
68 ? $self->_default_values( $element, $data->{$name} )
69 14 ▶ : $element->default( $data->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{$name} eq 'HASH' ? : 4 10 14
70 }
71 elsif ( $element->is_block ) {
72 $self->_default_values( $element,
73 $nested_name
74 28 ▶ ? $data->{$nested_name}
Branch: 1 branch
BranchTrueFalseTotal
$nested_name ? : 4 24 28
75 : $data );
76 }
77 else {
78 64 ▶ if ( $self->inflators && @{ $element->get_inflators } > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->inflators and @{$element->get_inflators;} > 0) { } 4 60 64
Condition66%2/3
$self->inflators and @{$element->get_inflators;} > 0
!ll&&!rl&&r
0604
MC/DC50%1/2
$self->inflators and @{$element->get_inflators;} > 0
$self->inflators @{$element->get_inflators;} > 0
Truth table66%2/3
A and B
A= $self->inflatorsB= @{$element->get_inflators;} > 0
ABresult
0X0
100
111
79 4 my @inflators = @{ $element->get_inflators };
80 4 map { $element->default( $_->process( $data->{$name} ) ) }
81 @inflators;
82 }
83 else {
84  
85 60 $element->default( $data->{$name} );
86 }
87 }
88  
89 }
90  
91 51 return $self;
92  
93 }
94  
95 0 ▶sub update { shift->create(@_) }
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
96  
97 8 ▶sub create {
Subroutine create: called CC=3 SCAR=11.0
Pod documented
98 8 my $self = shift;
99 8 ▶ if ( $self->form->submitted ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form->submitted) 2 6 8
100 2 my $input = _escape_hash( $self->form->input );
101 2 my $hf = Hash::Flatten->new(
102 { ArrayDelimiter => '_', HashDelimiter => '.' } );
103 2 $input = _unescape_hash( $hf->unflatten( $self->form->input ) );
104 2 $self->default_values(
105 $self->_unfold_repeatable( $self->form, $input ) );
106 }
107 8 $self->form->render_data;
108 8 my $obj = $self->_as_object_get( $self->form );
109 7 ▶ if ( $self->flatten ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->flatten) 1 6 7
110 1 my $hf = Hash::Flatten->new(
111 { ArrayDelimiter => '_', HashDelimiter => '.' } );
112 1 $obj = $self->_unfold_repeatable( $self->form, $hf->flatten($obj) );
113 }
114 7 return $obj;
115 }
116  
117 8 ▶sub _as_object_get {
Subroutine _as_object_get: called CC=21 SCAR=30.7
118 8 my $self = shift;
119 8 my $form = shift;
120 8 my $e = $form->get_all_elements;
121 8 my $names = {};
122 8 foreach my $element ( @{$e} ) {
123 156 my $name = $element->nested_name;
124 156 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 26 130 156
125 130 ▶ next if ( $element->type eq "Multi" );
Branch: 1 branch
BranchTrueFalseTotal
if $element->type eq "Multi" 10 120 130
126 120 my $es_name = _escape_name($name);
127 120 ▶ if ( $self->options
Branch: 2 branches
BranchTrueFalseTotal
if ($self->options and $element->can('_options') and @{$element->_options;} > 0) { } 22 98 120
elsif ($element->is_field and $self->deflators) { } 77 21 98
Condition83%5/6
$self->options and $element->can("_options") and @{$element->_options;} > 0
!ll&&!rl&&r
98022
$self->options and $element->can("_options")
!ll&&!rl&&r
386022
MC/DC66%2/3
$self->options and $element->can("_options") and @{$element->_options;} > 0
$self->options $element->can("_options") @{$element->_options;} > 0
Truth table75%3/4
A and B and C
A= $self->optionsB= $element->can("_options")C= @{$element->_options;} > 0
ABCresult
0XX0
10X0
1100
1111
128 && $element->can('_options')
129 22 && @{ $element->_options } > 0 )
130 {
131 22 my @options = @{ $element->_options };
132 my @values
133 = ref $element->default eq "ARRAY"
134 22 ▶ ? @{ $element->default }
Branch: 1 branch
BranchTrueFalseTotal
ref $element->default eq 'ARRAY' ? : 2 20 22
135 : $element->default;
136 22 $names->{$es_name} = [];
137 22 foreach my $value (@values) {
138 my @option
139 24 ▶ = grep { defined $value && $_->{value} eq $value } @options;
Condition100%3/3
defined $value && $_->{'value'} eq $value
!ll&&!rl&&r
13425216
MC/DC100%2/2
defined $value && $_->{'value'} eq $value
defined $value $_->{'value'} eq $value
Truth table100%3/3
A && B
A= defined $valueB= $_->{'value'} eq $value
ABresult
0X0
100
111
140 24 ▶ unless (@option) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@option) 8 16 24
141 8 ▶ @options = map { @{ $_->{group} || [] } } @options;
Condition50%1/2
$_->{'group'} || []
l!l
0134
MC/DC0%0/1
$_->{'group'} || []
$_->{'group'}
Truth table50%1/2
A
A= $_->{'group'}
Aresult
11
00
142 0 @option = grep { $_->{value} eq $value } @options;
143 }
144 my $obj
145 24 = [ map { { value => $_->{value}, label => $_->{label} } }
146 @option ];
147  
148 24 ▶ push( @{ $names->{$es_name} }, $obj->[0] ) if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 24 0 24
149 }
150 22 ▶ $names->{$es_name} = $names->{$es_name}->[0] if scalar @values == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @values == 1 20 2 22
151 22 ▶ $names->{$es_name} ||= { value => undef, label => undef };
Condition100%2/2
$names->{$es_name} ||= {"value", undef, "label", undef}
l!l
148
MC/DC100%1/1
$names->{$es_name} ||= {"value", undef, "label", undef}
$names->{$es_name}
Truth table100%2/2
A
A= $names->{$es_name}
Aresult
11
00
152 }
153 ▶ elsif ( $element->is_field && $self->deflators ) {
Condition66%2/3
$element->is_field and $self->deflators
!ll&&!rl&&r
21077
MC/DC50%1/2
$element->is_field and $self->deflators
$element->is_field $self->deflators
Truth table66%2/3
A and B
A= $element->is_fieldB= $self->deflators
ABresult
0X0
100
111
154 77 my $deflators = $element->get_deflators;
155 77 ▶ $names->{$es_name} = $element->default
Branch: 1 branch
BranchTrueFalseTotal
if $element->can("default") 77 0 77
156 if ( $element->can('default') );
157 4 map { $names->{$es_name} = $_->deflator( $names->{$es_name} ) }
158 77 @{$deflators};
159 }
160 else {
161 21 ▶ $names->{$es_name} = $element->default
Branch: 1 branch
BranchTrueFalseTotal
if $element->can("default") 0 21 21
162 if ( $element->can('default') );
163 }
164  
165 0 ▶ if ( blessed $names->{$es_name} ) { delete $names->{$es_name} }
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($names->{$es_name})) 0 120 120
166 }
167  
168 8 my $hf = Hash::Flatten->new( { ArrayDelimiter => '_' } );
169  
170 8 ▶ return $self->_unfold_repeatable( $form,
Branch: 1 branch
BranchTrueFalseTotal
$self->flatten ? : 1 7 8
171 $self->flatten ? $names : $hf->unflatten($names) );
172 }
173  
174 15 ▶sub _escape_hash {
Subroutine _escape_hash: called CC=6 SCAR=17.9
175 15 my $hash = shift;
176 15 ▶ my $method = shift || \&_escape_name;
Condition100%2/2
shift() || \&HTML::FormFu::Model::HashRef::_escape_name
l!l
123
MC/DC100%1/1
shift() || \&HTML::FormFu::Model::HashRef::_escape_name
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
177 15 ▶ return $hash unless ( ref $hash );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash 1 14 15
178 14 foreach my $k ( keys %$hash ) {
179 31 my $v = delete $hash->{$k};
180 31 ▶ if ( ref $v eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $v eq 'HASH') { } 4 27 31
elsif (ref $v eq 'ARRAY') { } 3 24 27
181 4 $hash->{ $method->($k) } = _escape_hash( $v, $method );
182 }
183 elsif ( ref $v eq 'ARRAY' ) {
184 $hash->{ $method->($k) }
185 3 = [ map { _escape_hash( $_, $method ) } @$v ];
186 }
187 else {
188 24 $hash->{ $method->($k) } = $v;
189 }
190 }
191 14 return $hash;
192 }
193  
194 3 ▶sub _unescape_hash {
Subroutine _unescape_hash: called CC=1 SCAR=0.0
195 3 return _escape_hash( shift, \&_unescape_name );
196 }
197  
198 140 ▶sub _escape_name {
Subroutine _escape_name: called CC=1 SCAR=0.0
199 140 my $name = shift;
200 140 $name =~ s/_/\\_/g;
201 140 $name =~ s/\\(_\d+(\.|$))/$1/g;
202 140 return $name;
203 }
204  
205 184 ▶sub _unescape_name {
Subroutine _unescape_name: called CC=1 SCAR=0.0
206 184 my $name = shift;
207 184 $name =~ s/\\_/_/g;
208 184 $name =~ s/\\\./\./g;
209 184 return $name;
210 }
211  
212 193 ▶sub _unfold_repeatable {
Subroutine _unfold_repeatable: called CC=13 SCAR=27.3
213 193 my $self = shift;
214 193 my $form = shift;
215 193 my $data = shift;
216 193 ▶ return $data unless ( ref $data eq "HASH" );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 151 42 193
217 42 my $new = {};
218  
219 42 ▶ while ( my ( $k, $v ) = each %{$data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$data;}) 168 41 209
220 168 my $key = _unescape_name($k);
221  
222 168 ▶ if ( $self->get_repeatable($key) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->get_repeatable($key)) { } 6 161 167
elsif ($self->get_multi($key) and ref $v eq 'ARRAY') { } 0 161 161
223 6 $new->{$key} = [];
224  
225 # iterate over all array elements
226 # we ignore the first one (index 0) as it is undef as we start
227 # counting the repeated element names with 1 and the automatic
228 # from Hash::Flatten assumed 0 as first index while unflattening
229 # the parameter names
230 # Example:
231 # $v = [
232 # undef,
233 # {
234 # 'foo' => 'bar',
235 # 'id' => 1
236 # },
237 # {
238 # 'foo' => 'baz',
239 # 'id' => 2
240 # }
241 # ];
242 6 ▶ for ( my $i = 1; $i < @{ $v || [] }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$v or [];}) 11 6 17
Condition50%1/2
$v || []
l!l
170
MC/DC0%0/1
$v || []
$v
Truth table50%1/2
A
A= $v
Aresult
11
00
243  
244 # process all key value pairs in an array element
245 11 ▶ while ( my ( $name, $values ) = each %{ $v->[$i] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$v[$i];}) 21 11 32
246  
247 # add an empty hash to array of unfolded data if not already present
248 11 push( @{ $new->{$key} }, {} )
249 21 ▶ unless $new->{$key}->[ $i - 1 ];
Branch: 1 branch
BranchTrueFalseTotal
unless $new->{$key}[$i - 1] 11 10 21
250  
251 # store processed values
252 21 $new->{$key}->[ $i - 1 ]->{$name}
253 = $self->_unfold_repeatable( $form, $values );
254 }
255 }
256 }
257 ▶ elsif ( $self->get_multi($key) && ref $v eq "ARRAY" ) {
Condition66%2/3
$self->get_multi($key) and ref $v eq "ARRAY"
!ll&&!rl&&r
145160
MC/DC0%0/2
$self->get_multi($key) and ref $v eq "ARRAY"
$self->get_multi($key) ref $v eq "ARRAY"
Truth table66%2/3
A and B
A= $self->get_multi($key)B= ref $v eq "ARRAY"
ABresult
0X0
100
111
258 0 ▶ for ( @{ $v || [] } ) {
Condition0%0/2
$v || []
l!l
00
MC/DC0%0/1
$v || []
$v
Truth table0%0/2
A
A= $v
Aresult
11
00
259 0 $new->{$key} = $_;
260 0 ▶ last if $new->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $new->{$key} 0 0 0
261 }
262 }
263 else {
264 161 $new->{$key} = $self->_unfold_repeatable( $form, $v );
265 }
266 }
267  
268 41 return $new;
269 }
270  
271 161 ▶sub get_multi {
Subroutine get_multi: called CC=4 SCAR=13.9
Pod undocumented
272 161 my $self = shift;
273 161 my $element = shift;
274 161 ▶ unless ( $self->_multi ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_multi) 4 157 161
275 4 my %multis = ();
276 4 my $multis = $self->form->get_all_elements( { type => qr/Multi/ } );
277 4 ▶ foreach my $multi ( @{ $multis || [] } ) {
Condition50%1/2
$multis || []
l!l
40
MC/DC0%0/1
$multis || []
$multis
Truth table50%1/2
A
A= $multis
Aresult
11
00
278 4 my @multis;
279 4 map { push( @multis, $_->name ) } @{ $multi->get_elements };
280 4 map { my $i = $_; $i =~ s/_\d+//; $multis{$i} = 1 } @multis;
281 }
282 4 $self->_multi( \%multis );
283 }
284 161 return $self->_multi->{$element};
285  
286 }
287  
288 168 ▶sub get_repeatable {
Subroutine get_repeatable: called CC=5 SCAR=16.1
Pod undocumented
289 168 my $self = shift;
290 168 my $element = shift;
291 168 ▶ unless ( $self->_repeatable ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_repeatable) 5 163 168
292 5 my %rep = ();
293 5 my $rep = $self->form->get_all_elements( { type => qr/Repeatable/ } );
294  
295 # TODO - Mario Minati 19.05.2009
296 # use $_->delimiter to split the keys
297 5 ▶ foreach my $rep_element ( @{ $rep || [] } ) {
Condition50%1/2
$rep || []
l!l
50
MC/DC0%0/1
$rep || []
$rep
Truth table50%1/2
A
A= $rep
Aresult
11
00
298 4 my $name = $rep_element->nested_name;
299 4 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 3 4
300 "A Repeatable element without a nested_name attribute cannot be handled by Model::HashRef"
301 unless $name;
302 3 $name =~ s/_\d+//;
303 3 $rep{$name} = 1;
304 }
305 4 $self->_repeatable( \%rep );
306 }
307 167 return $self->_repeatable->{$element};
308  
309 }
310  
311 7 __PACKAGE__->meta->make_immutable;
312  
313 __END__
« lib/HTML/FormFu/Util.pm ↑ Summary lib/HTML/FormFu/Constraint/MinMaxFields.pm »