lib/HTML/FormFu/Element/Select.pm

filter: Statementstmt 100% 51 / 51 Branchbran 100% 16 / 16 Conditioncond 87.5% 21 / 24 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 94.6% 106 / 112 SCAR 29.5
CC
19
Cov
97%
_prepare_attrs
15.1
_string_field
5.0
BEGIN
1.0
CRAP 19.0
« lib/HTML/FormFu/Constraint/MinMaxFields.pm ↑ Summary lib/HTML/FormFu/Constraint/Repeatable/Any.pm »
1 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Element::Select;
4 51 $HTML::FormFu::Element::Select::VERSION = '2.06';
5 # ABSTRACT: Select form field
6  
7 51 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 51 extends 'HTML::FormFu::Element';
9  
10 51 with 'HTML::FormFu::Role::Element::Group';
11  
12 51 ▶use HTML::FormFu::Constants qw( $EMPTY_STR );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 51 ▶use HTML::FormFu::Util qw( append_xml_attribute process_attrs );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 51 ▶use List::Util 1.33 qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 51 __PACKAGE__->mk_attr_accessors(qw( multiple size ));
17  
18 after BUILD => sub {
19 236 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 236 $self->layout_field_filename('field_layout_select_field');
22 236 $self->multi_value(1);
23  
24 236 return;
25 51 };
26  
27 sub _prepare_attrs {
28 8014 ▶ my ( $self, $submitted, $value, $default, $option ) = @_;
Subroutine _prepare_attrs: called CC=15 SCAR=27.1
29  
30 8014 ▶ 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'}) { } 151 7863 8014
elsif ($submitted and $self->retain_default and !defined($value) || $value eq $HTML::FormFu::Element::Select::EMPTY_STR and defined $self->value and $self->value eq $option->{'value'}) { } 4 7859 7863
elsif ($submitted) { } 3380 4479 7859
elsif (defined $default and ref $default eq 'ARRAY' ? &any(sub { $_ eq $option->{'value'}; } , @$default) : $default eq $option->{'value'}) { } 191 5083 5274
ref $value eq 'ARRAY' ? : 3 3046 3049
Condition100%6/6
$submitted and defined $value and ref $value eq "ARRAY" ? &any(sub {
!ll&&!rl&&r
49652898151
$submitted and defined $value
!ll&&!rl&&r
44794863049
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
31 && defined $value
32 && (ref $value eq 'ARRAY'
33 5 ▶ ? any { $_ eq $option->{value} } @$value
Subroutine __ANON__: called CC=1 SCAR=0.0
34 : $value eq $option->{value} ) )
35 {
36 151 $option->{attributes}{selected} = 'selected';
37 }
38 ▶ elsif ($submitted
Condition80%12/15
$submitted and $self->retain_default and !defined($value) || $value eq $HTML::FormFu::Element::Select::EMPTY_STR and defined $self->value and $self->value eq $option->{"value"}
!ll&&!rl&&r
785184
$submitted and $self->retain_default and !defined($value) || $value eq $HTML::FormFu::Element::Select::EMPTY_STR and defined $self->value
!ll&&!rl&&r
7851012
$submitted and $self->retain_default and !defined($value) || $value eq $HTML::FormFu::Element::Select::EMPTY_STR
!ll&&!rl&&r
7851012
$submitted and $self->retain_default
!ll&&!rl&&r
4479337212
not defined $value or $value eq $HTML::FormFu::Element::Select::EMPTY_STR
l!l&&r!l&&!r
660
MC/DC50%3/6
$submitted and $self->retain_default and !defined($value) || $value eq $HTML::FormFu::Element::Select::EMPTY_STR and defined $self->value and $self->value eq $option->{"value"}
$submitted $self->retain_default not defined $value $value eq $HTML::FormFu::Element::Select::EMPTY_STR defined $self->value $self->value eq $option->{"value"}
Truth table66%6/9
A and B and not(C) or D and E and F
A= $submittedB= $self->retain_defaultC= not defined $valueD= $value eq $HTML::FormFu::Element::Select::EMPTY_STRE= defined $self->valueF= $self->value eq $option->{"value"}
ABCDEFresult
0XXXXX0
10XXXX0
1100XX0
111X0X0
11010X0
111X100
1101100
111X111
1101111
39 && $self->retain_default
40 && ( !defined $value || $value eq $EMPTY_STR )
41 && defined( $self->value )
42 && $self->value eq $option->{value} )
43 {
44 4 $option->{attributes}{selected} = 'selected';
45 }
46 elsif ($submitted) {
47 3380 delete $option->{attributes}{selected};
48 }
49 ▶ elsif (
Branch: 1 branch
BranchTrueFalseTotal
ref $default eq 'ARRAY' ? : 29 3655 3684
Condition100%3/3
defined $default and ref $default eq "ARRAY" ? &any(sub {
!ll&&!rl&&r
7953493191
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
50 defined $default
51 && (ref $default eq 'ARRAY'
52 46 ▶ ? any { $_ eq $option->{value} } @$default
Subroutine __ANON__: called CC=1 SCAR=0.0
53 : $default eq $option->{value} ) )
54 {
55 191 $option->{attributes}{selected} = 'selected';
56 }
57 8014 return;
58 }
59  
60 sub _string_field {
61 233 ▶ my ( $self, $render ) = @_;
Subroutine _string_field: called CC=5 SCAR=16.1
62  
63 # select_tag template
64  
65 my $html .= sprintf qq{<select name="%s"%s>\n},
66 $render->{nested_name},
67 233 process_attrs( $render->{attributes} );
68  
69 233 for my $option ( @{ $render->{options} } ) {
70 3953 ▶ if ( exists $option->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{'group'}) { } 14 3939 3953
71 14 $html .= "<optgroup";
72  
73 14 ▶ if ( defined $option->{label} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option->{"label"}) 8 6 14
74 8 $html .= sprintf qq{ label="%s"}, $option->{label};
75 }
76  
77 14 $html .= sprintf "%s>\n", process_attrs( $option->{attributes} );
78  
79 14 for my $item ( @{ $option->{group} } ) {
80 $html .= sprintf qq{<option value="%s"%s>%s</option>\n},
81 $item->{value},
82 process_attrs( $item->{attributes} ),
83 $item->{label},
84 28 ;
85 }
86  
87 14 $html .= "</optgroup>\n";
88 }
89 else {
90 $html .= sprintf qq{<option value="%s"%s>%s</option>\n},
91 $option->{value},
92 process_attrs( $option->{attributes} ),
93 $option->{label},
94 3939 ;
95 }
96 }
97  
98 233 $html .= "</select>";
99  
100 233 return $html;
101 }
102  
103 51 __PACKAGE__->meta->make_immutable;
104  
105 51 1;
106  
107 __END__
108  
109 =pod
110  
111 =encoding UTF-8
112  
113 =head1 NAME
114  
115 HTML::FormFu::Element::Select - Select form field
116  
117 =head1 VERSION
118  
119 version 2.06
120  
121 =head1 SYNOPSIS
122  
123 YAML config:
124  
125 ---
126 elements:
127 - type: Select
128 name: sex
129 options:
130 - [ 'm', 'Male' ]
131 - [ 'f', 'Female' ]
132  
133 =head1 DESCRIPTION
134  
135 Select form field.
136  
137 Supports optgroups, see L<HTML::FormFu::Role::Element::Group/options> for
138 details.
139  
140 =head1 METHODS
141  
142 =head2 options
143  
144 See L<HTML::FormFu::Role::Element::Group/options>.
145  
146 =head2 values
147  
148 See L<HTML::FormFu::Role::Element::Group/values>.
149  
150 =head2 value_range
151  
152 See L<HTML::FormFu::Role::Element::Group/value_range>.
153  
154 =head2 empty_first
155  
156 See L<HTML::FormFu::Role::Element::Group/empty_first>.
157  
158 =head2 empty_first_label
159  
160 See L<HTML::FormFu::Role::Element::Group/empty_first_label>.
161  
162 =head1 SEE ALSO
163  
164 Is a sub-class of, and inherits methods from
165 L<HTML::FormFu::Role::Element::Group>,
166 L<HTML::FormFu::Role::Element::Field>,
167 L<HTML::FormFu::Element>
168  
169 L<HTML::FormFu>
170  
171 =head1 AUTHOR
172  
173 Carl Franks, C<cfranks@cpan.org>
174  
175 =head1 LICENSE
176  
177 This library is free software, you can redistribute it and/or modify it under
178 the same terms as Perl itself.
179  
180 =head1 AUTHOR
181  
182 Carl Franks <cpan@fireartist.com>
183  
184 =head1 COPYRIGHT AND LICENSE
185  
186 This software is copyright (c) 2018 by Carl Franks.
187  
188 This is free software; you can redistribute it and/or modify it under
189 the same terms as the Perl 5 programming language system itself.
190  
191 =cut
« lib/HTML/FormFu/Constraint/MinMaxFields.pm ↑ Summary lib/HTML/FormFu/Constraint/Repeatable/Any.pm »