lib/HTML/FormFu/Role/Element/Group.pm

filter: Statementstmt 89.8% 160 / 178 Branchbran 78.8% 71 / 90 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 95.4% 21 / 22 Podpod n/a totaltotal 87.3% 263 / 301 CC 64 SCAR 29.5
CC
64
Cov
87%
_parse_option_hashref
41.0
__ANON__
13.0
options
7.1
CRAP 19.1
« lib/HTML/FormFu/QueryType/CGI/Simple.pm ↑ Summary lib/HTML/FormFu/Role/Element/Field.pm »
1 75 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Role::Element::Group;
4 75 $HTML::FormFu::Role::Element::Group::VERSION = '2.09';
5 # ABSTRACT: Role for grouped form fields
6  
7 75 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 75 with 'HTML::FormFu::Role::Element::Field',
10 'HTML::FormFu::Role::Element::SingleValueField',
11 'HTML::FormFu::Role::Element::ProcessOptionsFromModel',
12 'HTML::FormFu::Role::Element::Coercible';
13  
14 75 ▶use HTML::FormFu::Attribute qw( mk_output_accessors );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 75 ▶use HTML::FormFu::Util qw( append_xml_attribute literal xml_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 75 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 75 ▶use List::Util 1.33 qw( none );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 75 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 75 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 75 has empty_first => ( is => 'rw', traits => ['Chained'] );
22  
23 75 __PACKAGE__->mk_output_accessors(qw( empty_first_label ));
24  
25 has _options => (
26 is => 'rw',
27 5 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 75 lazy => 1,
29 isa => 'ArrayRef',
30 );
31  
32 75 my @ALLOWED_OPTION_KEYS = qw(
33 group
34 value
35 value_xml
36 value_loc
37 label
38 label_xml
39 label_loc
40 attributes
41 attrs
42 attributes_xml
43 attrs_xml
44 container_attributes
45 container_attrs
46 container_attributes_xml
47 container_attrs_xml
48 label_attributes
49 label_attrs
50 label_attributes_xml
51 label_attrs_xml
52 );
53  
54 313 ▶after BUILD => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 313 my $self = shift;
56  
57 313 $self->container_attributes( {} );
58  
59 313 return;
60 75 };
61  
62 273 ▶after process => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 273 my $self = shift;
64  
65 273 $self->_process_options_from_model;
66  
67 273 return;
68 75 };
69  
70 250 ▶sub options {
Subroutine options: called CC=7 SCAR=19.6
71 250 my ( $self, $arg ) = @_;
72 250 my ( @options, @new );
73  
74 250 ▶ return $self->_options if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 25 225 250
75  
76 225 ▶ croak "options argument must be a single array-ref" if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 225 225
77  
78 225 ▶ if ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 225 0 225
79 225 ▶ croak "options argument must be an array-ref"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($arg) ne "ARRAY" 0 225 225
80 if reftype($arg) ne 'ARRAY';
81  
82 225 @options = @$arg;
83  
84 225 ▶ if ( $self->empty_first ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->empty_first) 6 219 225
85 6 push @new, $self->_get_empty_first_option;
86 }
87  
88 225 for my $item (@options) {
89 3963 push @new, $self->_parse_option($item);
90 }
91 }
92  
93 223 $self->_options( \@new );
94  
95 223 return $self;
96 }
97  
98 20 ▶sub _get_empty_first_option {
Subroutine _get_empty_first_option: called CC=2 SCAR=6.9
99 20 my ($self) = @_;
100  
101 20 ▶ my $label = $self->empty_first_label || '';
Condition100%2/2
$self->empty_first_label || ''
l!l
416
MC/DC100%1/1
$self->empty_first_label || ''
$self->empty_first_label
Truth table100%2/2
A
A= $self->empty_first_label
Aresult
11
00
102  
103 return {
104 20 value => '',
105 label => $label,
106 attributes => {},
107 container_attributes => {},
108 label_attributes => {},
109 };
110 }
111  
112 3985 ▶sub _parse_option {
Subroutine _parse_option: called CC=3 SCAR=11.3
113 3985 my ( $self, $item ) = @_;
114  
115 3985 ▶ if ( reftype($item) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($item) eq 'HASH') { } 38 3947 3985
elsif (builtin::reftype($item) eq 'ARRAY') { } 3947 0 3947
116 38 return $self->_parse_option_hashref($item);
117 }
118 elsif ( reftype($item) eq 'ARRAY' ) {
119 return {
120 3947 value => $item->[0],
121 label => $item->[1],
122 attributes => {},
123 container_attributes => {},
124 label_attributes => {},
125 };
126 }
127 else {
128 0 croak "each options argument must be a hash-ref or array-ref";
129 }
130 }
131  
132 38 ▶sub _parse_option_hashref {
Subroutine _parse_option_hashref: called CC=32 SCAR=37.1
133 38 my ( $self, $item ) = @_;
134  
135 # sanity check options
136 38 my @keys = keys %$item;
137  
138 38 for my $key (@keys) {
139 croak "unknown option argument: '$key'"
140 87 ▶ if none { $key eq $_ } @ALLOWED_OPTION_KEYS;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $key eq $_; } , @ALLOWED_OPTION_KEYS) 1 86 87
141  
142 86 my $short = $key;
143  
144 86 ▶ if ( $short =~ s/attributes/attrs/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($short =~ s/attributes/attrs/) 20 66 86
145 20 for my $cmp (@keys) {
146 74 ▶ next if $cmp eq $key;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp eq $key 19 55 74
147  
148 55 ▶ croak "cannot use both '$key' and '$short' arguments"
Branch: 1 branch
BranchTrueFalseTotal
if $cmp eq $short 1 54 55
149 if $cmp eq $short;
150 }
151 }
152 }
153  
154 36 ▶ if ( exists $item->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{"group"}) 11 25 36
155 11 my @group = @{ $item->{group} };
156 11 my @new;
157 11 for my $groupitem (@group) {
158 22 push @new, $self->_parse_option($groupitem);
159 }
160 11 $item->{group} = \@new;
161 }
162  
163 36 ▶ if ( !exists $item->{attributes} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $item->{"attributes"}) 26 10 36
164 $item->{attributes}
165 = exists $item->{attrs}
166 ? $item->{attrs}
167 26 ▶ : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $item->{'attrs'} ? : 1 25 26
168 }
169  
170 36 ▶ if ( exists $item->{attributes_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $item->{'attributes_xml'}) { } 3 33 36
elsif (exists $item->{'attrs_xml'}) { } 1 32 33
171 3 for my $key ( keys %{ $item->{attributes_xml} } ) {
172 $item->{attributes}{$key}
173 3 = literal( $item->{attributes_xml}{$key} );
174 }
175 }
176 elsif ( exists $item->{attrs_xml} ) {
177 1 for my $key ( keys %{ $item->{attrs_xml} } ) {
178 1 $item->{attributes}{$key} = literal( $item->{attrs_xml}{$key} );
179 }
180 }
181  
182 36 ▶ if ( !exists $item->{container_attributes} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $item->{"container_attributes"}) 32 4 36
183 $item->{container_attributes}
184 = exists $item->{container_attrs}
185 ? $item->{container_attrs}
186 32 ▶ : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $item->{'container_attrs'} ? : 0 32 32
187 }
188  
189 36 ▶ if ( exists $item->{container_attributes_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $item->{'container_attributes_xml'}) { } 0 36 36
elsif (exists $item->{'container_attrs_xml'}) { } 0 36 36
190 0 for my $key ( keys %{ $item->{container_attributes_xml} } ) {
191 $item->{container_attributes}{$key}
192 0 = literal( $item->{container_attributes_xml}{$key} );
193 }
194 }
195 elsif ( exists $item->{container_attrs_xml} ) {
196 0 for my $key ( keys %{ $item->{container_attrs_xml} } ) {
197 $item->{container_attributes}{$key}
198 0 = literal( $item->{container_attrs_xml}{$key} );
199 }
200 }
201  
202 36 ▶ if ( !exists $item->{label_attributes} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $item->{"label_attributes"}) 34 2 36
203 $item->{label_attributes}
204 = exists $item->{label_attrs}
205 ? $item->{label_attrs}
206 34 ▶ : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $item->{'label_attrs'} ? : 0 34 34
207 }
208  
209 36 ▶ if ( exists $item->{label_attributes_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $item->{'label_attributes_xml'}) { } 0 36 36
elsif (exists $item->{'label_attrs_xml'}) { } 0 36 36
210 0 for my $key ( keys %{ $item->{label_attributes_xml} } ) {
211 $item->{label_attributes}{$key}
212 0 = literal( $item->{label_attributes_xml}{$key} );
213 }
214 }
215 elsif ( exists $item->{label_attrs_xml} ) {
216 0 for my $key ( keys %{ $item->{label_attrs_xml} } ) {
217 $item->{label_attributes}{$key}
218 0 = literal( $item->{label_attrs_xml}{$key} );
219 }
220 }
221  
222 36 ▶ if ( defined $item->{label_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $item->{'label_xml'}) { } 2 34 36
elsif (defined $item->{'label_loc'}) { } 1 33 34
223 2 $item->{label} = literal( $item->{label_xml} );
224 }
225 elsif ( defined $item->{label_loc} ) {
226 1 $item->{label} = $self->form->localize( $item->{label_loc} );
227 }
228  
229 36 ▶ if ( defined $item->{value_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $item->{'value_xml'}) { } 2 34 36
elsif (defined $item->{'value_loc'}) { } 0 34 34
230 2 $item->{value} = literal( $item->{value_xml} );
231 }
232 elsif ( defined $item->{value_loc} ) {
233 0 $item->{value} = $self->form->localize( $item->{value_loc} );
234 }
235  
236 36 ▶ if ( !defined $item->{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item->{"value"}) 12 24 36
237 12 $item->{value} = '';
238 }
239  
240 36 return $item;
241 }
242  
243 55 ▶sub values {
Subroutine values: called CC=5 SCAR=16.3
244 55 my ( $self, $arg ) = @_;
245  
246 55 ▶ croak "values argument must be a single array-ref of values" if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 55 55
247  
248 55 my @values;
249  
250 55 ▶ if ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 55 0 55
251 55 ▶ croak "values argument must be an array-ref"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($arg) ne "ARRAY" 0 55 55
252 if reftype($arg) ne 'ARRAY';
253  
254 55 @values = @$arg;
255 }
256  
257 my @new = map {
258 55 { value => $_,
259 label => ucfirst $_,
260 attributes => {},
261 container_attributes => {},
262 label_attributes => {},
263 }
264 } @values;
265  
266 55 ▶ if ( $self->empty_first ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->empty_first) 14 41 55
267 14 unshift @new, $self->_get_empty_first_option;
268 }
269  
270 55 $self->_options( \@new );
271  
272 55 return $self;
273 }
274  
275 3 ▶sub value_range {
Subroutine value_range: called CC=5 SCAR=16.6
276 3 my ( $self, $arg ) = @_;
277 3 my (@values);
278  
279 3 ▶ croak "value_range argument must be a single array-ref of values"
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 3 3
280 if @_ > 2;
281  
282 3 ▶ if ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 3 0 3
283 3 ▶ croak "value_range argument must be an array-ref"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($arg) ne "ARRAY" 0 3 3
284 if reftype($arg) ne 'ARRAY';
285  
286 3 @values = @$arg;
287 }
288  
289 3 ▶ croak "range must contain at least 2 values" if @values < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @values < 2 0 3 3
290  
291 3 my $end = pop @values;
292 3 my $start = pop @values;
293  
294 3 return $self->values( [ @values, $start .. $end ] );
295 }
296  
297 492 ▶before prepare_attrs => sub {
Subroutine __ANON__: called CC=13 SCAR=25.6
298 492 my ( $self, $render ) = @_;
299  
300 492 my $submitted = $self->form->submitted;
301 492 my $default = $self->default;
302  
303 492 ▶ my $value
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name ? : 477 15 492
304 = defined $self->name
305 ? $self->get_nested_hash_value( $self->form->input, $self->nested_name )
306 : undef;
307  
308 492 ▶ if ( ( reftype($value) || '' ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($value) || "") eq "ARRAY") 6 486 492
Condition100%2/2
builtin::reftype($value) || ''
l!l
6486
MC/DC100%1/1
builtin::reftype($value) || ''
builtin::reftype($value)
Truth table100%2/2
A
A= builtin::reftype($value)
Aresult
11
00
309 6 my $elems
310 = $self->form->get_fields( { nested_name => $self->nested_name } );
311 6 ▶ if ($#$elems) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elems->$#*) 4 2 6
312  
313 # There are multiple fields with the same name; assume
314 # none are multi-value fields, i.e. only one selected
315 # option per field. (Otherwise it might be ambiguous
316 # which option came from which field.)
317 4 for ( 0 .. @$elems - 1 ) {
318 8 ▶ if ( $self == $elems->[$_] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self == $elems->[$_]) 4 4 8
319  
320 # Use the value of the option actually selected in
321 # this group.
322 4 $value = $value->[$_];
323 }
324 }
325 }
326 }
327  
328 492 ▶ if ( !$submitted && defined $default ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $submitted and defined $default) 187 305 492
Condition100%3/3
not $submitted and defined $default
!ll&&!rl&&r
21689187
MC/DC100%2/2
not $submitted and defined $default
not $submitted defined $default
Truth table100%3/3
not(A) and B
A= not $submittedB= defined $default
ABresult
0X0
100
111
329 187 for my $deflator ( @{ $self->_deflators } ) {
330 1 $default = $deflator->process($default);
331 }
332 }
333  
334 492 for my $option ( @{ $render->{options} } ) {
335 8294 ▶ if ( exists $option->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{'group'}) { } 29 8265 8294
336 29 for my $item ( @{ $option->{group} } ) {
337 58 $self->_prepare_attrs( $submitted, $value, $default, $item );
338 }
339 }
340 else {
341 8265 $self->_prepare_attrs( $submitted, $value, $default, $option );
342 }
343 }
344  
345 492 return;
346 75 };
347  
348 492 ▶around render_data_non_recursive => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
349 492 my ( $orig, $self, $args ) = @_;
350  
351 492 ▶ my $render = $self->$orig(
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 0 492 492
352 { options => Clone::clone( $self->_options ),
353 $args ? %$args : (),
354 } );
355  
356 492 $self->_quote_options( $render->{options} );
357  
358 492 return $render;
359 75 };
360  
361 521 ▶sub _quote_options {
Subroutine _quote_options: called CC=3 SCAR=11.0
362 521 my ( $self, $options ) = @_;
363  
364 521 foreach my $opt (@$options) {
365 8352 $opt->{label} = xml_escape( $opt->{label} );
366 8352 $opt->{value} = xml_escape( $opt->{value} );
367 8352 $opt->{attributes} = xml_escape( $opt->{attributes} );
368 8352 $opt->{label_attributes} = xml_escape( $opt->{label_attributes} );
369 $opt->{container_attributes}
370 8352 = xml_escape( $opt->{container_attributes} );
371  
372 8352 ▶ if ( exists $opt->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{"group"}) 29 8323 8352
373 29 $self->_quote_options( $opt->{group} );
374 }
375 }
376 }
377  
378 0 ▶around clone => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
379 0 my ( $orig, $self ) = @_;
380  
381 0 my $clone = $self->$orig(@_);
382  
383 0 $clone->_options( Clone::clone( $self->_options ) );
384  
385 0 return $clone;
386 75 };
387  
388 75 1;
389  
390 __END__
« lib/HTML/FormFu/QueryType/CGI/Simple.pm ↑ Summary lib/HTML/FormFu/Role/Element/Field.pm »