FormHandler/InitResult.pm

filter: Statementstmt 96.7% 89 / 92 Branchbran 73.6% 53 / 72 Conditioncond 70.1% 40 / 57 MC/DCmcdc 33.3% 11 / 33 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 76.3% 200 / 262 CC 59 SCAR 28.1
CC
59
Cov
82%
_get_value
19.3
_result_from_fields
18.0
_result_from_object
16.7
CRAP 16.6
« FormHandler/Pages.pm ↑ Summary FormHandler/Widget/Wrapper/Bootstrap3.pm »
1 package HTML::FormHandler::InitResult;
2 # ABSTRACT: internal code
3 147 $HTML::FormHandler::InitResult::VERSION = '0.410002';
4 147 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 # _init is for building fields when
8 # there is no initial object and no params
9 # formerly _init
10 461 ▶sub _result_from_fields {
Subroutine _result_from_fields: called CC=14 SCAR=28.9
11 461 my ( $self, $self_result ) = @_;
12  
13 # defaults for compounds, etc.
14 461 ▶ if ( my @values = $self->get_default_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@values) = $self->get_default_value) 7 454 461
15 7 ▶ my $value = @values > 1 ? \@values : shift @values;
Branch: 1 branch
BranchTrueFalseTotal
@values > 1 ? : 0 7 7
16 7 ▶ if( ref $value eq 'HASH' || blessed $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH" or builtin::blessed($value)) 7 0 7
Condition66%2/3
ref $value eq "HASH" or builtin::blessed($value)
l!l&&r!l&&!r
610
MC/DC0%0/2
ref $value eq "HASH" or builtin::blessed($value)
ref $value eq "HASH" builtin::blessed($value)
Truth table66%2/3
A or B
A= ref $value eq "HASH"B= builtin::blessed($value)
ABresult
1X1
011
000
17 7 return $self->_result_from_object( $self_result, $value );
18 }
19 0 ▶ $self->init_value($value) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
20 0 ▶ $self_result->_set_value($value) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
21 }
22 454 my $my_value;
23 454 for my $field ( $self->sorted_fields ) {
24 1318 ▶ next if ($field->inactive && !$field->_active);
Branch: 1 branch
BranchTrueFalseTotal
if $field->inactive and not $field->_active 0 1318 1318
Condition66%2/3
$field->inactive and not $field->_active
!ll&&!rl&&r
131530
MC/DC0%0/2
$field->inactive and not $field->_active
$field->inactive not $field->_active
Truth table66%2/3
A and not(B)
A= $field->inactiveB= not $field->_active
ABresult
0X0
100
111
25 1318 my $result = HTML::FormHandler::Field::Result->new(
26 name => $field->name,
27 parent => $self_result
28 );
29 1318 $result = $field->_result_from_fields($result);
30 1318 ▶ $my_value->{ $field->name } = $result->value if $result->has_value;
Branch: 1 branch
BranchTrueFalseTotal
if $result->has_value 85 1233 1318
31 1318 ▶ $self_result->add_result($result) if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 1318 0 1318
32 }
33 # setting value here to handle disabled compound fields, where we want to
34 # preserve the 'value' because the fields aren't submitted...except for the
35 # form. Not sure it's the best idea to skip for form, but it maintains previous behavior
36 454 ▶ $self_result->_set_value($my_value) if ( keys %$my_value );
Branch: 1 branch
BranchTrueFalseTotal
if keys %$my_value 62 392 454
37 454 $self->_set_result($self_result);
38 454 ▶ $self_result->_set_field_def($self) if $self->DOES('HTML::FormHandler::Field');
Branch: 1 branch
BranchTrueFalseTotal
if $self->DOES("HTML::FormHandler::Field") 130 324 454
39 454 return $self_result;
40 }
41  
42 # building fields from input (params)
43 # formerly done in validate_field
44 274 ▶sub _result_from_input {
Subroutine _result_from_input: called CC=12 SCAR=27.4
45 274 my ( $self, $self_result, $input, $exists ) = @_;
46  
47 # transfer the input values to the input attributes of the
48 # subfields
49 274 ▶ return unless ( defined $input || $exists || $self->has_fields );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input or $exists or $self->has_fields 0 274 274
Condition33%2/6
defined $input or $exists or $self->has_fields
l!l&&r!l&&!r
27400
defined $input or $exists
l!l&&r!l&&!r
27400
MC/DC0%0/3
defined $input or $exists or $self->has_fields
defined $input $exists $self->has_fields
Truth table25%1/4
A or B or C
A= defined $inputB= $existsC= $self->has_fields
ABCresult
1XX1
01X1
0011
0000
50 274 $self_result->_set_input($input);
51 274 ▶ if ( ref $input eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input eq "HASH") 274 0 274
52 274 foreach my $field ( $self->sorted_fields ) {
53 843 ▶ next if ($field->inactive && !$field->_active);
Branch: 1 branch
BranchTrueFalseTotal
if $field->inactive and not $field->_active 0 843 843
Condition66%2/3
$field->inactive and not $field->_active
!ll&&!rl&&r
83940
MC/DC0%0/2
$field->inactive and not $field->_active
$field->inactive not $field->_active
Truth table66%2/3
A and not(B)
A= $field->inactiveB= not $field->_active
ABresult
0X0
100
111
54 843 my $field_name = $field->name;
55 843 my $result = HTML::FormHandler::Field::Result->new(
56 name => $field_name,
57 parent => $self_result
58 );
59 $result =
60 $field->_result_from_input( $result, $input->{$field->input_param || $field_name},
61 843 ▶ exists $input->{$field->input_param || $field_name} );
Condition66%4/6
$field->input_param || $field_name
l!l&&r!l&&!r
28410
$field->input_param || $field_name
l!l&&r!l&&!r
28410
MC/DC0%0/2
$field->input_param || $field_name
$field->input_param $field_name
MC/DC0%0/2
$field->input_param || $field_name
$field->input_param $field_name
Truth table66%2/3
A || B
A= $field->input_paramB= $field_name
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $field->input_paramB= $field_name
ABresult
1X1
011
000
62 843 ▶ $self_result->add_result($result) if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 843 0 843
63 }
64 }
65 274 $self->_set_result($self_result);
66 274 ▶ $self_result->_set_field_def($self) if $self->DOES('HTML::FormHandler::Field');
Branch: 1 branch
BranchTrueFalseTotal
if $self->DOES("HTML::FormHandler::Field") 97 177 274
67 274 return $self_result;
68 }
69  
70 # building fields from model object or init_obj hash
71 # formerly _init_from_object
72 115 ▶sub _result_from_object {
Subroutine _result_from_object: called CC=16 SCAR=28.1
73 115 my ( $self, $self_result, $item ) = @_;
74  
75 115 ▶ return unless ( $item || $self->has_fields ); # empty fields for compounds
Branch: 1 branch
BranchTrueFalseTotal
unless $item or $self->has_fields 0 115 115
Condition33%1/3
$item or $self->has_fields
l!l&&r!l&&!r
11500
MC/DC0%0/2
$item or $self->has_fields
$item $self->has_fields
Truth table33%1/3
A or B
A= $itemB= $self->has_fields
ABresult
1X1
011
000
76 115 my $my_value;
77 115 my $init_obj = $self->form->init_object;
78 115 for my $field ( $self->sorted_fields ) {
79 333 ▶ next if ( $field->inactive && !$field->_active );
Branch: 1 branch
BranchTrueFalseTotal
if $field->inactive and not $field->_active 0 333 333
Condition33%1/3
$field->inactive and not $field->_active
!ll&&!rl&&r
33300
MC/DC0%0/2
$field->inactive and not $field->_active
$field->inactive not $field->_active
Truth table33%1/3
A and not(B)
A= $field->inactiveB= not $field->_active
ABresult
0X0
100
111
80 333 my $result = HTML::FormHandler::Field::Result->new(
81 name => $field->name,
82 parent => $self_result
83 );
84 333 ▶ if ( (ref $item eq 'HASH' && !exists $item->{ $field->accessor } ) ||
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq 'HASH' and not exists $$item{$field->accessor} or builtin::blessed($item) and not $item->can($field->accessor)) { } 58 275 333
Condition100%9/9
ref $item eq "HASH" and not exists $$item{$field->accessor} or builtin::blessed($item) and not $item->can($field->accessor)
l!l&&r!l&&!r
499275
ref $item eq "HASH" and not exists $$item{$field->accessor}
!ll&&!rl&&r
2625849
builtin::blessed($item) and not $item->can($field->accessor)
!ll&&!rl&&r
258179
MC/DC75%3/4
ref $item eq "HASH" and not exists $$item{$field->accessor} or builtin::blessed($item) and not $item->can($field->accessor)
ref $item eq "HASH" not exists $$item{$field->accessor} builtin::blessed($item) not $item->can($field->accessor)
Truth table57%4/7
A and not(B) or C and not(D)
A= ref $item eq "HASH"B= not exists $$item{$field->accessor}C= builtin::blessed($item)D= not $item->can($field->accessor)
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
85 ( blessed($item) && !$item->can($field->accessor) ) ) {
86 58 my $found = 0;
87 58 ▶ if ($field->form->use_init_obj_when_no_accessor_in_item) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->form->use_init_obj_when_no_accessor_in_item) 6 52 58
88 # if we're using an item, look for accessor not found in item
89 # in the init_object
90 6 my @names = split( /\./, $field->full_name );
91 6 my $init_obj_value = $self->find_sub_item( $init_obj, \@names );
92 6 ▶ if ( defined $init_obj_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $init_obj_value) 4 2 6
93 4 $found = 1;
94 4 $result = $field->_result_from_object( $result, $init_obj_value );
95 }
96 }
97 58 ▶ $result = $field->_result_from_fields($result) unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 54 4 58
98 }
99 else {
100 275 my $value;
101 275 ▶ $value = $self->_get_value( $field, $item ) unless $field->writeonly;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->writeonly 275 0 275
102 275 $result = $field->_result_from_object( $result, $value );
103 }
104 333 ▶ $self_result->add_result($result) if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 333 0 333
105 333 $my_value->{ $field->name } = $field->value;
106 }
107 115 $self_result->_set_value($my_value);
108 115 $self->_set_result($self_result);
109 115 ▶ $self_result->_set_field_def($self) if $self->DOES('HTML::FormHandler::Field');
Branch: 1 branch
BranchTrueFalseTotal
if $self->DOES("HTML::FormHandler::Field") 50 65 115
110 115 return $self_result;
111 }
112  
113 # this is used for reloading repeatables form the database if they've changed and
114 # for finding field values in the init_object when we have an item and the
115 # 'use_init_obj_when_no_accessor_in_item' flag is set
116 7 ▶sub find_sub_item {
Subroutine find_sub_item: called CC=2 SCAR=7.1
Pod undocumented
117 7 my ( $self, $item, $field_name_array ) = @_;
118 7 my $this_fname = shift @$field_name_array;;
119 7 my $field = $self->field($this_fname);
120 7 my $new_item = $self->_get_value( $field, $item );
121 7 ▶ if ( scalar @$field_name_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$field_name_array) 0 7 7
122 0 $new_item = $field->find_sub_item( $new_item, $field_name_array );
123 }
124 7 return $new_item;
125 }
126  
127 282 ▶sub _get_value {
Subroutine _get_value: called CC=19 SCAR=29.6
128 282 my ( $self, $field, $item ) = @_;
129  
130 282 my $accessor = $field->accessor;
131 282 my @values;
132 282 ▶ if( defined $field->default_over_obj ) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined $field->default_over_obj) { } 3 279 282
elsif ($field->form and $field->form->use_defaults_over_obj and @values = $field->get_default_value) { } 2 277 279
elsif (builtin::blessed($item) and $item->can($accessor)) { } 17 260 277
elsif (exists $item->{$accessor}) { } 258 2 260
elsif (@values = $field->get_default_value) { } 0 2 2
133 3 @values = $field->default_over_obj;
134 }
135 ▶ elsif( $field->form && $field->form->use_defaults_over_obj && ( @values = $field->get_default_value ) ) {
Condition83%5/6
$field->form and $field->form->use_defaults_over_obj and @values = $field->get_default_value
!ll&&!rl&&r
27612
$field->form and $field->form->use_defaults_over_obj
!ll&&!rl&&r
02763
MC/DC66%2/3
$field->form and $field->form->use_defaults_over_obj and @values = $field->get_default_value
$field->form $field->form->use_defaults_over_obj @values = $field->get_default_value
Truth table75%3/4
A and B and C
A= $field->formB= $field->form->use_defaults_over_objC= @values = $field->get_default_value
ABCresult
0XX0
10X0
1100
1111
136 }
137 ▶ elsif ( blessed($item) && $item->can($accessor) ) {
Condition66%2/3
builtin::blessed($item) and $item->can($accessor)
!ll&&!rl&&r
260017
MC/DC50%1/2
builtin::blessed($item) and $item->can($accessor)
builtin::blessed($item) $item->can($accessor)
Truth table66%2/3
A and B
A= builtin::blessed($item)B= $item->can($accessor)
ABresult
0X0
100
111
138 # this must be an array, so that DBIx::Class relations are arrays not resultsets
139 17 @values = $item->$accessor;
140 # for non-DBIC blessed object where access returns arrayref
141 17 ▶ if ( scalar @values == 1 && ref $values[0] eq 'ARRAY' && $field->has_flag('multiple') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @values == 1 and ref $values[0] eq "ARRAY" and $field->has_flag("multiple")) 1 16 17
Condition66%4/6
scalar @values == 1 and ref $values[0] eq "ARRAY" and $field->has_flag("multiple")
!ll&&!rl&&r
1601
scalar @values == 1 and ref $values[0] eq "ARRAY"
!ll&&!rl&&r
0161
MC/DC33%1/3
scalar @values == 1 and ref $values[0] eq "ARRAY" and $field->has_flag("multiple")
scalar @values == 1 ref $values[0] eq "ARRAY" $field->has_flag("multiple")
Truth table50%2/4
A and B and C
A= scalar @values == 1B= ref $values[0] eq "ARRAY"C= $field->has_flag("multiple")
ABCresult
0XX0
10X0
1100
1111
142 1 @values = @{$values[0]};
143 }
144 }
145 elsif ( exists $item->{$accessor} ) {
146 258 my $v = $item->{$accessor};
147 258 ▶ if($field->has_flag('multiple') && ref($v) eq 'ARRAY'){
Branch: 1 branch
BranchTrueFalseTotal
if ($field->has_flag('multiple') and ref $v eq 'ARRAY') { } 3 255 258
Condition100%3/3
$field->has_flag("multiple") and ref $v eq "ARRAY"
!ll&&!rl&&r
25233
MC/DC100%2/2
$field->has_flag("multiple") and ref $v eq "ARRAY"
$field->has_flag("multiple") ref $v eq "ARRAY"
Truth table100%3/3
A and B
A= $field->has_flag("multiple")B= ref $v eq "ARRAY"
ABresult
0X0
100
111
148 3 @values = @$v;
149 } else {
150 255 @values = $v;
151 }
152 }
153 elsif ( @values = $field->get_default_value ) {
154 }
155 else {
156 2 return;
157 }
158 280 ▶ if( $field->has_inflate_default_method ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->has_inflate_default_method) 9 271 280
159 9 @values = $field->inflate_default(@values);
160 }
161 280 my $value;
162 280 ▶ if( $field->has_flag('multiple')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->has_flag('multiple')) { } 7 273 280
163 7 ▶ $value = scalar @values == 1 && ! defined $values[0] ? [] : \@values;
Branch: 1 branch
BranchTrueFalseTotal
scalar @values == 1 && !defined($values[0]) ? : 1 6 7
Condition100%3/3
scalar @values == 1 and not defined $values[0]
!ll&&!rl&&r
241
MC/DC100%2/2
scalar @values == 1 and not defined $values[0]
scalar @values == 1 not defined $values[0]
Truth table100%3/3
A and not(B)
A= scalar @values == 1B= not defined $values[0]
ABresult
0X0
100
111
164 }
165 else {
166 273 ▶ $value = @values > 1 ? \@values : shift @values;
Branch: 1 branch
BranchTrueFalseTotal
@values > 1 ? : 0 273 273
167 }
168 280 return $value;
169 }
170  
171 147 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 147 1;
173  
174 __END__
« FormHandler/Pages.pm ↑ Summary FormHandler/Widget/Wrapper/Bootstrap3.pm »