lib/HTML/FormFu/Model/HashRef.pm

filter: Statementstmt 95.7% 182 / 190 Branchbran 84.8% 73 / 86 Conditioncond 82.7% 24 / 29 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 94.1% 16 / 17 Podpod 60.0% 3 / 5 totaltotal 89.8% 309 / 344 SCAR 41.6
CC
62
Cov
91%
_as_object_get
21.6
_default_values
16.0
_unfold_repeatable
15.3
CRAP 64.4
« lib/HTML/FormFu/Role/Element/Group.pm ↑ Summary lib/HTML/FormFu/Element/Date.pm »
1 package HTML::FormFu::Model::HashRef;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 our $VERSION = '2.05'; # VERSION
5  
6 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use MooseX::Attribute::FormFuChained;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 extends 'HTML::FormFu::Model';
10  
11 6 ▶use Hash::Flatten;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 has flatten => ( is => 'rw' );
15 6 has options => ( is => 'rw' );
16  
17 6 has _repeatable => ( is => 'rw', traits => ['FormFuChained'] );
18 6 has _multi => ( is => 'rw', traits => ['FormFuChained'] );
19  
20 6 has deflators => (
21 is => 'rw',
22 default => 1,
23 lazy => 1,
24 traits => ['FormFuChained'],
25 );
26  
27 6 has inflators => (
28 is => 'rw',
29 default => 1,
30 lazy => 1,
31 traits => ['FormFuChained'],
32 );
33  
34 sub default_values {
35 8 ▶ my ( $self, $data ) = @_;
Subroutine default_values: called CC=1 SCAR=0.0
Pod documented
36 94 map { $_->default(undef) }
37 8 ( grep { $_->is_field } @{ $self->form->get_all_elements } );
38 8 $self->_default_values( $self->form, $data );
39 8 return $self;
40 }
41  
42 sub _default_values {
43 51 ▶ my ( $self, $form, $data ) = @_;
Subroutine _default_values: called CC=16 SCAR=27.7
44 51 my $elements = $form->get_elements;
45 51 foreach my $element ( @{$elements} ) {
46 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
47 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
48 113 ▶ $name =~ s/_\d+$// if ($name);
Branch: 1 branch
BranchTrueFalseTotal
if $name 85 28 113
49 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
50 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
51 7 ▶ unless ($value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value) 1 6 7
52 1 $element->repeat(0);
53 0 map { $element->remove_element($_) }
54 1 @{ $element->get_elements };
55 1 next;
56 }
57 6 my $k = scalar @{$value};
58 6 $element->repeat($k);
59 6 my $childs = $element->get_elements;
60 6 ▶ for ( my $i = 0; $i < $k; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $k) 11 6 17
61 11 $self->_default_values( $childs->[$i], $value->[$i] );
62 }
63 }
64 ▶ 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
65 { # is a Multi element
66 ref $data->{$name} eq "HASH"
67 ? $self->_default_values( $element, $data->{$name} )
68 14 ▶ : $element->default( $data->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{$name} eq 'HASH' ? : 4 10 14
69 }
70 elsif ( $element->is_block ) {
71 $self->_default_values( $element,
72 $nested_name
73 28 ▶ ? $data->{$nested_name}
Branch: 1 branch
BranchTrueFalseTotal
$nested_name ? : 4 24 28
74 : $data );
75 }
76 else {
77 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
78 4 my @inflators = @{ $element->get_inflators };
79 4 map { $element->default( $_->process( $data->{$name} ) ) }
80 @inflators;
81 }
82 else {
83  
84 60 $element->default( $data->{$name} );
85 }
86 }
87  
88 }
89  
90 51 return $self;
91  
92 }
93  
94 0 ▶sub update { shift->create(@_) }
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
95  
96 sub create {
97 8 ▶ my $self = shift;
Subroutine create: called CC=3 SCAR=11.0
Pod documented
98 8 ▶ if ( $self->form->submitted ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form->submitted) 2 6 8
99 2 my $input = _escape_hash( $self->form->input );
100 2 my $hf = Hash::Flatten->new(
101 { ArrayDelimiter => '_', HashDelimiter => '.' } );
102 2 $input = _unescape_hash( $hf->unflatten( $self->form->input ) );
103 2 $self->default_values(
104 $self->_unfold_repeatable( $self->form, $input ) );
105 }
106 8 $self->form->render_data;
107 8 my $obj = $self->_as_object_get( $self->form );
108 7 ▶ if ( $self->flatten ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->flatten) 1 6 7
109 1 my $hf = Hash::Flatten->new(
110 { ArrayDelimiter => '_', HashDelimiter => '.' } );
111 1 $obj = $self->_unfold_repeatable( $self->form, $hf->flatten($obj) );
112 }
113 7 return $obj;
114 }
115  
116 sub _as_object_get {
117 8 ▶ my $self = shift;
Subroutine _as_object_get: called CC=21 SCAR=30.7
118 8 my $form = shift;
119 8 my $e = $form->get_all_elements;
120 8 my $names = {};
121 8 foreach my $element ( @{$e} ) {
122 156 my $name = $element->nested_name;
123 156 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 26 130 156
124 130 ▶ next if ( $element->type eq "Multi" );
Branch: 1 branch
BranchTrueFalseTotal
if $element->type eq "Multi" 10 120 130
125 120 my $es_name = _escape_name($name);
126 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
127 && $element->can('_options')
128 22 && @{ $element->_options } > 0 )
129 {
130 22 my @options = @{ $element->_options };
131 my @values
132 = ref $element->default eq "ARRAY"
133 22 ▶ ? @{ $element->default }
Branch: 1 branch
BranchTrueFalseTotal
ref $element->default eq 'ARRAY' ? : 2 20 22
134 : $element->default;
135 22 $names->{$es_name} = [];
136 22 foreach my $value (@values) {
137 my @option
138 24 ▶ = grep { defined $value && $_->{value} eq $value } @options;
Branch: 1 branch
BranchTrueFalseTotal
defined $value and $_->{"value"} eq $value 268 134 402
139 24 ▶ unless (@option) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@option) 8 16 24
140 8 ▶ @options = map { @{ $_->{group} || [] } } @options;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"group"} or [] 134 0 134
141 8 @option = grep { $_->{value} eq $value } @options;
142 }
143 my $obj
144 24 = [ map { { value => $_->{value}, label => $_->{label} } }
145 @option ];
146  
147 24 ▶ push( @{ $names->{$es_name} }, $obj->[0] ) if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 24 0 24
148 }
149 22 ▶ $names->{$es_name} = $names->{$es_name}->[0] if scalar @values == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @values == 1 20 2 22
150 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
151 }
152 ▶ 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
153 77 my $deflators = $element->get_deflators;
154 77 ▶ $names->{$es_name} = $element->default
Branch: 1 branch
BranchTrueFalseTotal
if $element->can("default") 77 0 77
155 if ( $element->can('default') );
156 4 map { $names->{$es_name} = $_->deflator( $names->{$es_name} ) }
157 77 @{$deflators};
158 }
159 else {
160 21 ▶ $names->{$es_name} = $element->default
Branch: 1 branch
BranchTrueFalseTotal
if $element->can("default") 0 21 21
161 if ( $element->can('default') );
162 }
163  
164 120 ▶ if ( blessed $names->{$es_name} ) { delete $names->{$es_name} }
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($names->{$es_name})) 0 120 120
165 }
166  
167 8 my $hf = Hash::Flatten->new( { ArrayDelimiter => '_' } );
168  
169 8 ▶ return $self->_unfold_repeatable( $form,
Branch: 1 branch
BranchTrueFalseTotal
$self->flatten ? : 1 7 8
170 $self->flatten ? $names : $hf->unflatten($names) );
171 }
172  
173 sub _escape_hash {
174 15 ▶ my $hash = shift;
Subroutine _escape_hash: called CC=6 SCAR=17.9
175 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
176 15 ▶ return $hash unless ( ref $hash );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash 1 14 15
177 14 foreach my $k ( keys %$hash ) {
178 31 my $v = delete $hash->{$k};
179 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
180 4 $hash->{ $method->($k) } = _escape_hash( $v, $method );
181 }
182 elsif ( ref $v eq 'ARRAY' ) {
183 $hash->{ $method->($k) }
184 3 = [ map { _escape_hash( $_, $method ) } @$v ];
185 }
186 else {
187 24 $hash->{ $method->($k) } = $v;
188 }
189 }
190 14 return $hash;
191 }
192  
193 sub _unescape_hash {
194 3 ▶ return _escape_hash( shift, \&_unescape_name );
Subroutine _unescape_hash: called CC=1 SCAR=0.0
195 }
196  
197 sub _escape_name {
198 140 ▶ my $name = shift;
Subroutine _escape_name: called CC=1 SCAR=0.0
199 140 $name =~ s/_/\\_/g;
200 140 $name =~ s/\\(_\d+(\.|$))/$1/g;
201 140 return $name;
202 }
203  
204 sub _unescape_name {
205 184 ▶ my $name = shift;
Subroutine _unescape_name: called CC=1 SCAR=0.0
206 184 $name =~ s/\\_/_/g;
207 184 $name =~ s/\\\./\./g;
208 184 return $name;
209 }
210  
211 sub _unfold_repeatable {
212 193 ▶ my $self = shift;
Subroutine _unfold_repeatable: called CC=13 SCAR=27.3
213 193 my $form = shift;
214 193 my $data = shift;
215 193 ▶ return $data unless ( ref $data eq "HASH" );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 151 42 193
216 42 my $new = {};
217  
218 42 ▶ while ( my ( $k, $v ) = each %{$data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$data;}) 168 41 209
219 168 my $key = _unescape_name($k);
220  
221 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
222 6 $new->{$key} = [];
223  
224 # iterate over all array elements
225 # we ignore the first one (index 0) as it is undef as we start
226 # counting the repeated element names with 1 and the automatic
227 # from Hash::Flatten assumed 0 as first index while unflattening
228 # the parameter names
229 # Example:
230 # $v = [
231 # undef,
232 # {
233 # 'foo' => 'bar',
234 # 'id' => 1
235 # },
236 # {
237 # 'foo' => 'baz',
238 # 'id' => 2
239 # }
240 # ];
241 6 ▶ for ( my $i = 1; $i < @{ $v || [] }; $i++ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < @{$v or [];}) 11 6 17
$v or [] 0 17 17
242  
243 # process all key value pairs in an array element
244 11 ▶ while ( my ( $name, $values ) = each %{ $v->[$i] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$v[$i];}) 21 11 32
245  
246 # add an empty hash to array of unfolded data if not already present
247 11 push( @{ $new->{$key} }, {} )
248 21 ▶ unless $new->{$key}->[ $i - 1 ];
Branch: 1 branch
BranchTrueFalseTotal
unless $new->{$key}[$i - 1] 11 10 21
249  
250 # store processed values
251 21 $new->{$key}->[ $i - 1 ]->{$name}
252 = $self->_unfold_repeatable( $form, $values );
253 }
254 }
255 }
256 ▶ 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
257 0 ▶ for ( @{ $v || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$v or [] 0 0 0
258 0 $new->{$key} = $_;
259 0 ▶ last if $new->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $new->{$key} 0 0 0
260 }
261 }
262 else {
263 161 $new->{$key} = $self->_unfold_repeatable( $form, $v );
264 }
265 }
266  
267 41 return $new;
268 }
269  
270 sub get_multi {
271 161 ▶ my $self = shift;
Subroutine get_multi: called CC=4 SCAR=13.9
Pod undocumented
272 161 my $element = shift;
273 161 ▶ unless ( $self->_multi ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_multi) 4 157 161
274 4 my %multis = ();
275 4 my $multis = $self->form->get_all_elements( { type => qr/Multi/ } );
276 4 ▶ foreach my $multi ( @{ $multis || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$multis or [] 0 4 4
277 4 my @multis;
278 4 map { push( @multis, $_->name ) } @{ $multi->get_elements };
279 4 map { my $i = $_; $i =~ s/_\d+//; $multis{$i} = 1 } @multis;
280 }
281 4 $self->_multi( \%multis );
282 }
283 161 return $self->_multi->{$element};
284  
285 }
286  
287 sub get_repeatable {
288 168 ▶ my $self = shift;
Subroutine get_repeatable: called CC=5 SCAR=16.1
Pod undocumented
289 168 my $element = shift;
290 168 ▶ unless ( $self->_repeatable ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_repeatable) 5 163 168
291 5 my %rep = ();
292 5 my $rep = $self->form->get_all_elements( { type => qr/Repeatable/ } );
293  
294 # TODO - Mario Minati 19.05.2009
295 # use $_->delimiter to split the keys
296 5 ▶ foreach my $rep_element ( @{ $rep || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$rep or [] 0 5 5
297 4 my $name = $rep_element->nested_name;
298 4 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 3 4
299 "A Repeatable element without a nested_name attribute cannot be handled by Model::HashRef"
300 unless $name;
301 3 $name =~ s/_\d+//;
302 3 $rep{$name} = 1;
303 }
304 4 $self->_repeatable( \%rep );
305 }
306 167 return $self->_repeatable->{$element};
307  
308 }
309  
310 6 __PACKAGE__->meta->make_immutable;
311  
312 __END__
313  
314 =head1 NAME
315  
316 HTML::FormFu::Model::HashRef - handle hashrefs
317  
318 =head1 VERSION
319  
320 version 2.05
321  
322 =head1 SYNOPSIS
323  
324 ---
325 elements:
326 - user_id
327 - user_name
328 - type: Repeatable
329 nested_name: addresses
330 elements:
331 - type: Hidden
332 name: id
333 - street
334  
335  
336 $form->model('HashRef')->default_values( {
337 user_id => 123,
338 user_name => 'Hans',
339 addresses => [
340 { id => 2,
341 street => 'Somewhere' },
342 { id => 3,
343 street => 'Somewhere Else' }
344 ]
345 } );
346  
347 $form->default_model('HashRef');
348 my $hashref = $form->model->create();
349  
350 # $hashref is very much the same as the hashref you passed to default_values()
351  
352 =head1 DESCRIPTION
353  
354 If you need the content of a formular as hashref or for processing with other modules
355 like C<JSON> you can use this model.
356  
357 =head1 METHODS
358  
359 =head2 create
360  
361 This method creates a hashref from a filled form. This form can be filled by calling
362 L<HTML::FormFu/default_values>, default_values of any other model class (e. g. L<HTML::FormFu::Model::DBIC>)
363 or by simply submitting the form.
364  
365 If L</deflators> is true all deflators are processed (defaults to C<1>).
366  
367 If L</options> is true the value of all elements which have options like
368 L<HTML::FormFu::Element::Select> will be transformed.
369  
370 ---
371 elements:
372 - type: Select
373 name: select
374 options:
375 - [1, "Foo"]
376 - [2, "Bar"]
377  
378 If the value of C<select> is C<1>, L<create> will create this hashref:
379  
380 { 'select' => { label => 'Foo', value => 1 } }
381  
382 If there is more than one value selected, an arrayref is created instead:
383  
384 { 'select' => [ { label => 'Foo', value => 1 },
385 { label => 'Bar', value => 2 } ] }
386  
387 If L</options> is false, the output will look like this:
388  
389 { 'select' => 1 }
390  
391 respectively
392  
393 { 'select' => [1, 2] }
394  
395 L</options> is false by default.
396  
397 To get a flattened hash, you can set C</flatten> to a true value (defaults to C<0>).
398 This will generate a hash which uses the nested name of each field as key and the value
399 of this field as hash value. If there is a field which has more than one value,
400 a counter is added. The above example would result in a hash like this using C</flatten>:
401  
402 { 'select_0' => 1,
403 'select_1' => 2 }
404  
405  
406 =head2 update
407  
408 Alias for L</create>.
409  
410 =head2 default_values
411  
412 Populate a form using a hashref. This hashref has the same format as the output of L</create>.
413 If L</inflators> is true, all inflators will be processed (defaults to C<1>).
414  
415 =head1 CONFIGURATION
416  
417 These methods do not return the model object so chaining is not possible!
418  
419 =head2 options
420  
421 Adds the label of a value to the hashref if the element has L<HTML::FormFu::Role::Element::Group/options>.
422 See L</create> for an example. Defaults to C<0>.
423  
424 =head2 flatten
425  
426 Flattens the hash using L<Hash::Flatten>. See L</create> for an example. Defaults to C<0>.
427  
428 =head2 deflators
429  
430 If true, processes deflators in C</create>. Defaults to C<1>.
431  
432 =head2 inflators
433  
434 If true, processes inflators in C</default_values>. Defaults to C<1>.
435  
436 =head1 SEE ALSO
437  
438 L<HTML::FormFu>, L<Hash::Flatten>
439  
440 =head1 AUTHOR
441  
442 Moritz Onken, C<< onken@houseofdesign.de >>
« lib/HTML/FormFu/Role/Element/Group.pm ↑ Summary lib/HTML/FormFu/Element/Date.pm »