FormHandler/Widget/Field/Select.pm

filter: Statementstmt 98.5% 68 / 69 Branchbran 77.2% 17 / 22 Conditioncond 57.6% 15 / 26 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 5 totaltotal 75.1% 109 / 145 CC 23 SCAR 20.8
CC
23
Cov
85%
render_option
13.4
render_element
7.1
render_select_start
4.0
CRAP 8.0
« FormHandler/Widget/Theme/BootstrapFormMessages.pm ↑ Summary FormHandler/Field/Date.pm »
1 package HTML::FormHandler::Widget::Field::Select;
2 # ABSTRACT: select field rendering widget
3 32 $HTML::FormHandler::Widget::Field::Select::VERSION = '0.410001';
4  
5 32 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 32 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use HTML::FormHandler::Render::Util ('process_attrs');
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub render {
10 39 ▶ my ( $self, $result ) = @_;
Subroutine render: called CC=3 SCAR=11.2
Pod undocumented
11 39 ▶ $result ||= $self->result;
Condition66%2/3
$result ||= $self->result
l!l&&r!l&&!r
24150
MC/DC0%0/2
$result ||= $self->result
$result $self->result
Truth table66%2/3
A ||= B
A= $resultB= $self->result
ABresult
1X1
011
000
12 39 ▶ die "No result for form field '" . $self->full_name . "'. Field may be inactive." unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 39 39
13 39 my $output = $self->render_element( $result );
14 39 return $self->wrap_field( $result, $output );
15 }
16  
17 sub render_element {
18 40 ▶ my ( $self, $result ) = @_;
Subroutine render_element: called CC=7 SCAR=19.6
Pod undocumented
19 40 ▶ $result ||= $self->result;
Condition33%1/3
$result ||= $self->result
l!l&&r!l&&!r
4000
MC/DC0%0/2
$result ||= $self->result
$result $self->result
Truth table33%1/3
A ||= B
A= $resultB= $self->result
ABresult
1X1
011
000
20  
21 # create select element
22 40 my $output = $self->render_select_start( $result );
23  
24 # create empty select
25 40 ▶ if( defined $self->empty_select ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->empty_select) 3 37 40
26 3 $output .= $self->render_empty_select;
27 }
28  
29 # loop through options
30 40 foreach my $option ( @{ $self->{options} } ) {
31 132 ▶ if ( my $label = $option->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $label = $option->{'group'}) { } 3 129 132
32 3 ▶ $label = $self->_localize( $label ) if $self->localize_labels;
Branch: 1 branch
BranchTrueFalseTotal
if $self->localize_labels 0 3 3
33 3 $output .= qq{\n<optgroup label="$label">};
34 3 foreach my $group_opt ( @{ $option->{options} } ) {
35 9 $output .= $self->render_option( $group_opt, $result );
36 }
37 3 $output .= qq{\n</optgroup>};
38 }
39 else {
40 129 $output .= $self->render_option( $option, $result );
41 }
42 }
43 40 $self->reset_options_index;
44  
45 40 $output .= '</select>';
46 40 return $output;
47 }
48  
49 sub render_select_start {
50 40 ▶ my ( $self, $result ) = @_;
Subroutine render_select_start: called CC=4 SCAR=13.9
Pod undocumented
51 40 ▶ $result ||= $self->result;
Condition33%1/3
$result ||= $self->result
l!l&&r!l&&!r
4000
MC/DC0%0/2
$result ||= $self->result
$result $self->result
Truth table33%1/3
A ||= B
A= $resultB= $self->result
ABresult
1X1
011
000
52  
53 40 my $id = $self->id;
54 40 my $output = '<select name="' . $self->html_name . qq{" id="$id"};
55 40 ▶ $output .= ' multiple="multiple"' if $self->multiple;
Branch: 1 branch
BranchTrueFalseTotal
if $self->multiple 17 23 40
56 40 ▶ $output .= ' size="' . $self->size . '"' if defined $self->size;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->size 10 30 40
57 40 $output .= process_attrs($self->element_attributes($result));
58 40 $output .= '>';
59 40 return $output;
60 }
61  
62 sub render_empty_select {
63 3 ▶ my $self = shift;
Subroutine render_empty_select: called CC=1 SCAR=0.0
Pod undocumented
64  
65 3 my $label = $self->_localize($self->empty_select);
66 3 my $id = $self->id . "." . $self->options_index;
67 3 my $output = qq{\n<option value="" id="$id">$label</option>};
68 3 $self->inc_options_index;
69 3 return $output;
70 }
71  
72 sub render_option {
73 138 ▶ my ( $self, $option, $result ) = @_;
Subroutine render_option: called CC=12 SCAR=25.9
Pod undocumented
74 138 ▶ $result ||= $self->result;
Condition33%1/3
$result ||= $self->result
l!l&&r!l&&!r
13800
MC/DC0%0/2
$result ||= $self->result
$result $self->result
Truth table33%1/3
A ||= B
A= $resultB= $self->result
ABresult
1X1
011
000
75  
76 # current values
77 138 my $fif = $result->fif;
78 138 my %fif_lookup;
79 138 ▶ @fif_lookup{@$fif} = () if $self->multiple;
Branch: 1 branch
BranchTrueFalseTotal
if $self->multiple 65 73 138
80  
81 # not sure why the value of an option would be undef, but just in case,
82 # set to empty string because of 'eq' below
83 138 ▶ my $value = defined $option->{value} ? $option->{value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $option->{'value'} ? : 138 0 138
84 138 my $id = $self->id . '.' . $self->options_index;
85 138 my $output = qq{\n<option value="} . $self->html_filter($value) . '"';
86 138 $output .= qq{ id="$id"};
87  
88 # handle option attributes
89 138 ▶ my $attrs = $option->{attributes} || {};
Condition50%1/2
$option->{'attributes'} || {}
l!l
0138
MC/DC0%0/1
$option->{'attributes'} || {}
$option->{'attributes'}
Truth table50%1/2
A
A= $option->{'attributes'}
Aresult
11
00
90 138 ▶ if( defined $option->{disabled} && $option->{disabled} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option->{"disabled"} and $option->{"disabled"}) 0 138 138
Condition33%1/3
defined $option->{"disabled"} and $option->{"disabled"}
!ll&&!rl&&r
13800
MC/DC0%0/2
defined $option->{"disabled"} and $option->{"disabled"}
defined $option->{"disabled"} $option->{"disabled"}
Truth table33%1/3
A and B
A= defined $option->{"disabled"}B= $option->{"disabled"}
ABresult
0X0
100
111
91 0 $attrs->{disabled} = 'disabled';
92 }
93 138 ▶ if ( defined $fif &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fif and $self->multiple && exists $fif_lookup{$value} || $fif eq $value) 31 107 138
Condition88%8/9
defined $fif and $self->multiple && exists $fif_lookup{$value} || $fif eq $value
!ll&&!rl&&r
012810
$self->multiple and exists $fif_lookup{$value} or $fif eq $value
l!l&&r!l&&!r
2110107
$self->multiple and exists $fif_lookup{$value}
!ll&&!rl&&r
734421
MC/DC25%1/4
defined $fif and $self->multiple && exists $fif_lookup{$value} || $fif eq $value
defined $fif $self->multiple exists $fif_lookup{$value} $fif eq $value
Truth table50%3/6
A and B and C or D
A= defined $fifB= $self->multipleC= exists $fif_lookup{$value}D= $fif eq $value
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
94 ( ( $self->multiple && exists $fif_lookup{$value} ) ||
95 ( $fif eq $value ) ) ) {
96 31 $attrs->{selected} = 'selected';
97 }
98 138 $output .= process_attrs($attrs);
99  
100 # handle label
101 138 my $label = $option->{label};
102 138 ▶ $label = $self->_localize($label) if $self->localize_labels;
Branch: 1 branch
BranchTrueFalseTotal
if $self->localize_labels 0 138 138
103 138 $output .= '>' . ( $self->html_filter($label) ) . '</option>';
104 138 $self->inc_options_index;
105 138 return $output;
106 }
107  
108 32 1;
109  
110 __END__
111  
112 =pod
113  
114 =encoding UTF-8
115  
116 =head1 NAME
117  
118 HTML::FormHandler::Widget::Field::Select - select field rendering widget
119  
120 =head1 VERSION
121  
122 version 0.410001
123  
124 =head1 DESCRIPTION
125  
126 Renders single and multiple selects. Options hashrefs must
127 have 'value' and 'label' keys, and may have an 'attributes' key.
128  
129 See L<HTML::FormHandler::Field::Select> for documentation on
130 select fields and options.
131  
132 =head1 NAME
133  
134 HTML::FormHandler::Widget::Field::Select
135  
136 =head1 AUTHOR
137  
138 FormHandler Contributors - see HTML::FormHandler
139  
140 =head1 COPYRIGHT AND LICENSE
141  
142 This software is copyright (c) 2017 by Gerda Shank.
143  
144 This is free software; you can redistribute it and/or modify it under
145 the same terms as the Perl 5 programming language system itself.
146  
147 =cut
« FormHandler/Widget/Theme/BootstrapFormMessages.pm ↑ Summary FormHandler/Field/Date.pm »