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 package HTML::FormFu::Element::Select;
2  
3 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 51 our $VERSION = '2.05'; # VERSION
5  
6 51 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 51 extends 'HTML::FormFu::Element';
8  
9 51 with 'HTML::FormFu::Role::Element::Group';
10  
11 51 ▶use HTML::FormFu::Constants qw( $EMPTY_STR );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 51 ▶use HTML::FormFu::Util qw( append_xml_attribute process_attrs );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 51 ▶use List::Util 1.33 qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 51 __PACKAGE__->mk_attr_accessors(qw( multiple size ));
16  
17 after BUILD => sub {
18 236 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
19  
20 236 $self->layout_field_filename('field_layout_select_field');
21 236 $self->multi_value(1);
22  
23 236 return;
24 51 };
25  
26 sub _prepare_attrs {
27 8014 ▶ my ( $self, $submitted, $value, $default, $option ) = @_;
Subroutine _prepare_attrs: called CC=15 SCAR=27.1
28  
29 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
30 && defined $value
31 && (ref $value eq 'ARRAY'
32 5 ▶ ? any { $_ eq $option->{value} } @$value
Subroutine __ANON__: called CC=1 SCAR=0.0
33 : $value eq $option->{value} ) )
34 {
35 151 $option->{attributes}{selected} = 'selected';
36 }
37 ▶ 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
38 && $self->retain_default
39 && ( !defined $value || $value eq $EMPTY_STR )
40 && defined( $self->value )
41 && $self->value eq $option->{value} )
42 {
43 4 $option->{attributes}{selected} = 'selected';
44 }
45 elsif ($submitted) {
46 3380 delete $option->{attributes}{selected};
47 }
48 ▶ 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
49 defined $default
50 && (ref $default eq 'ARRAY'
51 46 ▶ ? any { $_ eq $option->{value} } @$default
Subroutine __ANON__: called CC=1 SCAR=0.0
52 : $default eq $option->{value} ) )
53 {
54 191 $option->{attributes}{selected} = 'selected';
55 }
56 8014 return;
57 }
58  
59 sub _string_field {
60 233 ▶ my ( $self, $render ) = @_;
Subroutine _string_field: called CC=5 SCAR=16.1
61  
62 # select_tag template
63  
64 my $html .= sprintf qq{<select name="%s"%s>\n},
65 $render->{nested_name},
66 233 process_attrs( $render->{attributes} );
67  
68 233 for my $option ( @{ $render->{options} } ) {
69 3953 ▶ if ( exists $option->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{'group'}) { } 14 3939 3953
70 14 $html .= "<optgroup";
71  
72 14 ▶ if ( defined $option->{label} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option->{"label"}) 8 6 14
73 8 $html .= sprintf qq{ label="%s"}, $option->{label};
74 }
75  
76 14 $html .= sprintf "%s>\n", process_attrs( $option->{attributes} );
77  
78 14 for my $item ( @{ $option->{group} } ) {
79 $html .= sprintf qq{<option value="%s"%s>%s</option>\n},
80 $item->{value},
81 process_attrs( $item->{attributes} ),
82 $item->{label},
83 28 ;
84 }
85  
86 14 $html .= "</optgroup>\n";
87 }
88 else {
89 $html .= sprintf qq{<option value="%s"%s>%s</option>\n},
90 $option->{value},
91 process_attrs( $option->{attributes} ),
92 $option->{label},
93 3939 ;
94 }
95 }
96  
97 233 $html .= "</select>";
98  
99 233 return $html;
100 }
101  
102 51 __PACKAGE__->meta->make_immutable;
103  
104 51 1;
105  
106 __END__
107  
108 =head1 NAME
109  
110 HTML::FormFu::Element::Select - Select form field
111  
112 =head1 VERSION
113  
114 version 2.05
115  
116 =head1 SYNOPSIS
117  
118 YAML config:
119  
120 ---
121 elements:
122 - type: Select
123 name: sex
124 options:
125 - [ 'm', 'Male' ]
126 - [ 'f', 'Female' ]
127  
128 =head1 DESCRIPTION
129  
130 Select form field.
131  
132 Supports optgroups, see L<HTML::FormFu::Role::Element::Group/options> for
133 details.
134  
135 =head1 METHODS
136  
137 =head2 options
138  
139 See L<HTML::FormFu::Role::Element::Group/options>.
140  
141 =head2 values
142  
143 See L<HTML::FormFu::Role::Element::Group/values>.
144  
145 =head2 value_range
146  
147 See L<HTML::FormFu::Role::Element::Group/value_range>.
148  
149 =head2 empty_first
150  
151 See L<HTML::FormFu::Role::Element::Group/empty_first>.
152  
153 =head2 empty_first_label
154  
155 See L<HTML::FormFu::Role::Element::Group/empty_first_label>.
156  
157 =head1 SEE ALSO
158  
159 Is a sub-class of, and inherits methods from
160 L<HTML::FormFu::Role::Element::Group>,
161 L<HTML::FormFu::Role::Element::Field>,
162 L<HTML::FormFu::Element>
163  
164 L<HTML::FormFu>
165  
166 =head1 AUTHOR
167  
168 Carl Franks, C<cfranks@cpan.org>
169  
170 =head1 LICENSE
171  
172 This library is free software, you can redistribute it and/or modify it under
173 the same terms as Perl itself.
174  
175 =cut
« lib/HTML/FormFu/Constraint/MinMaxFields.pm ↑ Summary lib/HTML/FormFu/Constraint/Repeatable/Any.pm »