lib/HTML/FormFu/Element/Checkboxgroup.pm

filter: Statementstmt 97.1% 103 / 106 Branchbran 89.4% 34 / 38 Conditioncond 83.3% 25 / 30 MC/DCmcdc 66.6% 10 / 15 Subroutinesub 92.8% 13 / 14 Podpod n/a totaltotal 91.1% 185 / 203 CC 35 SCAR 20.6
CC
35
Cov
93%
_prepare_attrs
14.2
_string_field
8.0
_prepare_id
7.2
CRAP 7.8
« lib/HTML/FormFu/Deflator/PathClassFile.pm ↑ Summary lib/HTML/FormFu/Constraint/Regex.pm »
1 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Element::Checkboxgroup;
4 24 $HTML::FormFu::Element::Checkboxgroup::VERSION = '2.09';
5 # ABSTRACT: Group of checkbox form fields
6  
7 24 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 extends 'HTML::FormFu::Element';
10  
11 24 with 'HTML::FormFu::Role::Element::Group';
12  
13 24 ▶use HTML::FormFu::Constants qw( $EMPTY_STR );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 24 ▶use HTML::FormFu::Util qw( append_xml_attribute process_attrs );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 24 ▶use List::Util 1.33 qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 24 has input_type => (
18 is => 'rw',
19 default => 'checkbox',
20 lazy => 1,
21 traits => ['Chained'],
22 );
23  
24 24 has reverse_group => (
25 is => 'rw',
26 traits => ['Chained'],
27 );
28  
29 68 ▶after BUILD => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 68 my ( $self, $args ) = @_;
31  
32 68 $self->layout_field_filename('field_layout_checkboxgroup_field');
33 68 $self->label_tag('legend');
34 68 $self->container_tag('fieldset');
35 68 $self->multi_value(1);
36 68 $self->reverse_group(1);
37 68 $self->input_type('checkbox');
38  
39 68 $self->layout( [ 'label', 'errors', 'field', 'comment', 'javascript', ] );
40  
41 68 return;
42 24 };
43  
44 52 ▶sub prepare_id {
Subroutine prepare_id: called CC=6 SCAR=17.9
45 52 my ( $self, $render ) = @_;
46  
47 52 ▶ my $form_id = defined $self->form->id ? $self->form->id : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->form->id ? : 3 49 52
48 52 ▶ my $field_name = defined $self->nested_name ? $self->nested_name : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->nested_name ? : 48 4 52
49 52 my $count = 0;
50  
51 52 for my $option ( @{ $render->{options} } ) {
52 126 ▶ if ( exists $option->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{'group'}) { } 15 111 126
53 15 for my $item ( @{ $option->{group} } ) {
54 30 $self->_prepare_id( $item, $form_id, $field_name, \$count );
55 }
56 }
57 else {
58 111 $self->_prepare_id( $option, $form_id, $field_name, \$count );
59 }
60 }
61  
62 52 return;
63 }
64  
65 141 ▶sub _prepare_id {
Subroutine _prepare_id: called CC=7 SCAR=19.8
66 141 my ( $self, $option, $form_id, $field_name, $count_ref ) = @_;
67  
68 141 ▶ if ( !exists $option->{attributes}{id} && defined $self->auto_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $option->{"attributes"}{"id"} and defined $self->auto_id) 6 135 141
Condition66%2/3
not exists $option->{"attributes"}{"id"} and defined $self->auto_id
!ll&&!rl&&r
01356
MC/DC50%1/2
not exists $option->{"attributes"}{"id"} and defined $self->auto_id
not exists $option->{"attributes"}{"id"} defined $self->auto_id
Truth table66%2/3
not(A) and B
A= not exists $option->{"attributes"}{"id"}B= defined $self->auto_id
ABresult
0X0
100
111
69 6 my %string = (
70 f => $form_id,
71 n => $field_name,
72 );
73  
74 6 my $id = $self->auto_id;
75 6 $id =~ s/%([fn])/$string{$1}/g;
76 6 $id =~ s/%c/ ++$$count_ref /gex;
77 6 $id =~ s/%v/ $option->{value} /gex;
78  
79 6 ▶ if ( defined( my $count = $self->repeatable_count ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $count = $self->repeatable_count)) 0 6 6
80 0 $id =~ s/%r/$count/g;
81 }
82  
83 6 $option->{attributes}{id} = $id;
84 }
85  
86 # label "for" attribute
87 141 ▶ if ( exists $option->{label}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"label"} and exists $option->{"attributes"}{"id"} and not exists $option->{"label_attributes"}{"for"}) 6 135 141
Condition66%4/6
exists $option->{"label"} and exists $option->{"attributes"}{"id"} and not exists $option->{"label_attributes"}{"for"}
!ll&&!rl&&r
13506
exists $option->{"label"} and exists $option->{"attributes"}{"id"}
!ll&&!rl&&r
01356
MC/DC33%1/3
exists $option->{"label"} and exists $option->{"attributes"}{"id"} and not exists $option->{"label_attributes"}{"for"}
exists $option->{"label"} exists $option->{"attributes"}{"id"} not exists $option->{"label_attributes"}{"for"}
Truth table50%2/4
A and B and not(C)
A= exists $option->{"label"}B= exists $option->{"attributes"}{"id"}C= not exists $option->{"label_attributes"}{"for"}
ABCresult
0XX0
10X0
1100
1111
88 && exists $option->{attributes}{id}
89 && !exists $option->{label_attributes}{for} )
90 {
91 6 $option->{label_attributes}{for} = $option->{attributes}{id};
92 }
93  
94 141 return;
95 }
96  
97 141 ▶sub _prepare_attrs {
Subroutine _prepare_attrs: called CC=14 SCAR=26.5
98 141 my ( $self, $submitted, $value, $default, $option ) = @_;
99  
100 141 ▶ if ( $submitted
Branch: 5 branches
BranchTrueFalseTotal
if ($submitted and defined $value and ref $value eq 'ARRAY' ? &any(sub { $_ eq $option->{'value'}; } , @$value) : $value eq $option->{'value'}) { } 15 126 141
elsif ($submitted and $self->retain_default and !defined($value) || $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STR and $self->value eq $option->{'value'}) { } 4 122 126
elsif ($submitted) { } 56 66 122
elsif (defined $default and ref $default eq 'ARRAY' ? &any(sub { $_ eq $option->{'value'}; } , @$default) : $default eq $option->{'value'}) { } 8 101 109
ref $value eq 'ARRAY' ? : 2 61 63
Condition100%6/6
$submitted and defined $value and ref $value eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$value) : $value eq $option->{"value"}
!ll&&!rl&&r
784815
$submitted and defined $value
!ll&&!rl&&r
661263
MC/DC100%3/3
$submitted and defined $value and ref $value eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$value) : $value eq $option->{"value"}
$submitted defined $value ref $value eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$value) : $value eq $option->{"value"}
Truth table100%4/4
A and B and C
A= $submittedB= defined $valueC= ref $value eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$value) : $value eq $option->{"value"}
ABCresult
0XX0
10X0
1100
1111
101 && defined $value
102 && (ref $value eq 'ARRAY'
103 3 ▶ ? any { $_ eq $option->{value} } @$value
Subroutine __ANON__: called CC=1 SCAR=0.0
104 : $value eq $option->{value} ) )
105 {
106 15 $option->{attributes}{checked} = 'checked';
107 }
108 ▶ elsif ($submitted
Condition83%10/12
$submitted and $self->retain_default and !defined($value) || $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STR and $self->value eq $option->{"value"}
!ll&&!rl&&r
11484
$submitted and $self->retain_default and !defined($value) || $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STR
!ll&&!rl&&r
114012
$submitted and $self->retain_default
!ll&&!rl&&r
664812
not defined $value or $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STR
l!l&&r!l&&!r
660
MC/DC60%3/5
$submitted and $self->retain_default and !defined($value) || $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STR and $self->value eq $option->{"value"}
$submitted $self->retain_default not defined $value $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STR $self->value eq $option->{"value"}
Truth table85%6/7
A and B and not(C) or D and E
A= $submittedB= $self->retain_defaultC= not defined $valueD= $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STRE= $self->value eq $option->{"value"}
ABCDEresult
0XXXX0
10XXX0
1100X0
111X00
110100
111X11
110111
109 && $self->retain_default
110 && ( !defined $value || $value eq $EMPTY_STR )
111 && $self->value eq $option->{value} )
112 {
113 4 $option->{attributes}{checked} = 'checked';
114 }
115 elsif ($submitted) {
116 56 delete $option->{attributes}{checked};
117 }
118 ▶ elsif (
Branch: 1 branch
BranchTrueFalseTotal
ref $default eq 'ARRAY' ? : 0 23 23
Condition100%3/3
defined $default and ref $default eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$default) : $default eq $option->{"value"}
!ll&&!rl&&r
43158
MC/DC100%2/2
defined $default and ref $default eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$default) : $default eq $option->{"value"}
defined $default ref $default eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$default) : $default eq $option->{"value"}
Truth table100%3/3
A and B
A= defined $defaultB= ref $default eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$default) : $default eq $option->{"value"}
ABresult
0X0
100
111
119 defined $default
120 && (ref $default eq 'ARRAY'
121 0 ▶ ? any { $_ eq $option->{value} } @$default
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 : $default eq $option->{value} ) )
123 {
124 8 $option->{attributes}{checked} = 'checked';
125 }
126 141 return;
127 }
128  
129 52 ▶sub render_data_non_recursive {
Subroutine render_data_non_recursive: called CC=4 SCAR=13.9
130 52 my ( $self, $args ) = @_;
131  
132 52 ▶ my $render = $self->SUPER::render_data_non_recursive(
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 52 0 52
133 { field_filename => $self->field_filename,
134 reverse_group => $self->reverse_group,
135 input_type => $self->input_type,
136 $args ? %$args : (),
137 } );
138  
139 52 for my $item ( @{ $render->{options} } ) {
140 126 ▶ if ( exists $item->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{"group"}) 15 111 126
141 15 append_xml_attribute( $item->{attributes}, 'class', 'subgroup' );
142 }
143 }
144  
145 52 return $render;
146 }
147  
148 50 ▶sub _string_field {
Subroutine _string_field: called CC=8 SCAR=20.8
149 50 my ( $self, $render ) = @_;
150  
151 # radiogroup_tag template
152  
153 my $name_attr
154 = defined $render->{nested_name}
155 ? sprintf( qq{ name="%s"}, $render->{nested_name} )
156 50 ▶ : q{};
Branch: 1 branch
BranchTrueFalseTotal
defined $render->{'nested_name'} ? : 48 2 50
157  
158 50 my $html .= sprintf "<span%s>\n", process_attrs( $render->{attributes} );
159  
160 50 for my $option ( @{ $render->{options} } ) {
161 124 ▶ if ( defined $option->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option->{'group'}) { } 15 109 124
162 $html .= sprintf "<span%s>\n",
163 15 process_attrs( $option->{attributes} ),
164 ;
165  
166 15 ▶ if ( defined $option->{label} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option->{"label"}) 8 7 15
167 8 $html .= sprintf "<p>%s</p>\n", $option->{label};
168 }
169  
170 15 for my $item ( @{ $option->{group} } ) {
171 $html .= sprintf
172 "<span%s>\n",
173 30 process_attrs( $item->{container_attributes} );
174  
175 my $label = sprintf
176 "<label%s>%s</label>\n",
177 process_attrs( $item->{label_attributes} ),
178 $item->{label},
179 30 ;
180  
181 my $input = sprintf
182 qq{<input%s type="%s" value="%s"%s />\n},
183 $name_attr,
184 $render->{input_type},
185 $item->{value},
186 30 process_attrs( $item->{attributes} ),
187 ;
188  
189 30 ▶ if ( $render->{reverse_group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($render->{'reverse_group'}) { } 30 0 30
190 30 $html .= $input . $label;
191 }
192 else {
193 0 $html .= $label . $input;
194 }
195  
196 30 $html .= "</span>\n";
197 }
198  
199 15 $html .= "</span>\n";
200 }
201 else {
202 $html .= sprintf
203 "<span%s>\n",
204 109 process_attrs( $option->{container_attributes} );
205  
206 my $label = sprintf
207 "<label%s>%s</label>\n",
208 process_attrs( $option->{label_attributes} ),
209 $option->{label},
210 109 ;
211  
212 my $input = sprintf
213 qq{<input%s type="%s" value="%s"%s />\n},
214 $name_attr,
215 $render->{input_type},
216 $option->{value},
217 109 process_attrs( $option->{attributes} ),
218 ;
219  
220 109 ▶ if ( $render->{reverse_group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($render->{'reverse_group'}) { } 101 8 109
221 101 $html .= $input . $label;
222 }
223 else {
224 8 $html .= $label . $input;
225 }
226  
227 109 $html .= "</span>\n";
228 }
229 }
230  
231 50 $html .= "</span>";
232  
233 50 return $html;
234 }
235  
236 24 __PACKAGE__->meta->make_immutable;
237  
238 24 1;
239  
240 __END__
« lib/HTML/FormFu/Deflator/PathClassFile.pm ↑ Summary lib/HTML/FormFu/Constraint/Regex.pm »