FormBuilder/Select.pm

filter: Statementstmt 94.1% 48 / 51 Branchbran 66.6% 8 / 12 Conditioncond 62.5% 10 / 16 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 92.8% 13 / 14 Podpod 100% 4 / 4 totaltotal 81.3% 87 / 107 CC 14 SCAR 10.4
CC
14
Cov
84%
value
4.2
__ANON__
4.1
_option_html
4.0
CRAP 2.8
« FormBuilder/FieldSet.pm ↑ Summary
1 package HTML::FormBuilder::Select;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our $VERSION = '0.12'; ## VERSION
10  
11 =head1 NAME
12  
13 HTML::FormBuilder::Select - Select Element Handling for BOM Forms
14  
15 =cut
16  
17 =head1 Synopsis
18  
19 my $select = HTML::FormBuilder::Select->new(
20 id => 'my-select',
21 name => 'my_select',
22 options => [{value => 'foo', text => 'Foo'}, {value => 'bar', text => 'Bar'}],
23 values => qw(foo),
24 };
25 $select->values('bar'); # set only 'bar'
26 $select->values('foo', 'bar'); # set only 'foo'
27 my $html = $select->widget_html;
28 my $hidden_input_html = $select->hidden_html;
29  
30 =head1 PROPERTIES
31  
32 =head2 id - id property of form element
33  
34 =cut
35  
36 5 has id => (
37 is => 'ro',
38 isa => \&is_str,
39 lazy => 1,
40 builder => '_build_id'
41 );
42  
43 sub _build_id {
44 0 ▶ my $self = shift;
Subroutine _build_id: not called CC=1 SCAR=6.9
45 0 return $self->name;
46 }
47  
48 =head2 name - name property of form element
49  
50 =cut
51  
52 5 has name => (
53 is => 'ro',
54 isa => \&is_str,
55 required => '1'
56 );
57  
58 =head2 options - option arrayref to generate options
59  
60 =cut
61  
62 has options => (
63 is => 'rw',
64 isa => sub {
65 21 ▶ die "$_[0] is not ArrayRef[HashRef[Any]]"
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" and ref $_[0][0] eq "HASH" 0 21 21
Condition33%1/3
ref $_[0] eq "ARRAY" and ref $_[0][0] eq "HASH"
!ll&&!rl&&r
0021
MC/DC0%0/2
ref $_[0] eq "ARRAY" and ref $_[0][0] eq "HASH"
ref $_[0] eq "ARRAY" ref $_[0][0] eq "HASH"
Truth table33%1/3
A and B
A= ref $_[0] eq "ARRAY"B= ref $_[0][0] eq "HASH"
ABresult
0X0
100
111
66 unless (ref($_[0]) eq 'ARRAY' && ref($_[0][0]) eq 'HASH');
67 },
68 5 );
69  
70 =head2 values - values (by value) selected
71  
72 =cut
73  
74 has values => (
75 is => 'rw',
76 34 ▶ isa => sub { die "$_[0] is not a Arrayref" unless (ref($_[0]) eq 'ARRAY') },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 34 34
77 11 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 5 );
79  
80 =head2 value
81  
82 Actually just a method that grabs the first value from values
83  
84 =cut
85  
86 sub value {
87 29 ▶ my $self = shift;
Subroutine value: called CC=4 SCAR=14.3
Pod documented
88 29 my $val = shift;
89 29 my $values = $self->values;
90 29 ▶ return $values->[0] if defined $values and not defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $values and not defined $val 16 13 29
Condition66%2/3
defined $values and not defined $val
!ll&&!rl&&r
01316
MC/DC50%1/2
defined $values and not defined $val
defined $values not defined $val
Truth table66%2/3
A and not(B)
A= defined $valuesB= not defined $val
ABresult
0X0
100
111
91 13 ▶ return $self->values([$val]) if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 13 0 13
92 0 return;
93 }
94  
95 =head1 METHODS
96  
97 =head2 widget_html
98  
99 =cut
100  
101 sub widget_html {
102 18 ▶ my $self = shift;
Subroutine widget_html: called CC=2 SCAR=6.9
Pod documented
103 18 my $html = '<select id="' . $self->id . '" name="' . $self->name . '">';
104 18 $html .= $self->_option_html($_) for @{$self->options};
105 18 $html .= "</select>";
106 18 return $html;
107 }
108  
109 sub _option_html {
110 36 ▶ my ($self, $optionhash) = @_;
Subroutine _option_html: called CC=4 SCAR=13.9
111 36 my $value = $optionhash->{value};
112 36 ▶ my $text = $optionhash->{text} // $value;
Condition100%3/3
$optionhash->{"text"} // $value
l!l&&r!l&&!r
5301
MC/DC100%2/2
$optionhash->{"text"} // $value
$optionhash->{"text"} $value
Truth table100%3/3
A // B
A= $optionhash->{"text"}B= $value
ABresult
1X1
011
000
113 36 my $selected = '';
114 36 ▶ $optionhash->{disabled} //= '';
Condition100%2/2
$optionhash->{"disabled"} //= ""
l!l
234
MC/DC100%1/1
$optionhash->{"disabled"} //= ""
$optionhash->{"disabled"}
Truth table100%2/2
A
A= $optionhash->{"disabled"}
Aresult
11
00
115 36 ▶ $selected = ' SELECTED' if grep { $_ eq $value } @{$self->values};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $value;} @{$self->values;} 9 27 36
116 36 return qq|<option value="$value"$selected $optionhash->{disabled}>$text</option>|;
117 }
118  
119 =head2 hidden_html
120  
121 =cut
122  
123 sub hidden_html {
124 3 ▶ my $self = shift;
Subroutine hidden_html: called CC=2 SCAR=7.2
Pod documented
125 3 ▶ return '<input type="hidden" id="' . $self->id . '" name="' . $self->name . '" value="' . ($self->value // '') . '" />';
Condition50%1/2
$self->value // ""
l!l
30
MC/DC0%0/1
$self->value // ""
$self->value
Truth table50%1/2
A
A= $self->value
Aresult
11
00
126 }
127  
128 =head2 is_str
129  
130 =cut
131  
132 sub is_str {
133 40 ▶ die "$_[0] is not a string" unless defined($_[0]) && !ref($_[0]);
Subroutine is_str: called CC=3 SCAR=13.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and not ref $_[0] 0 40 40
Condition33%1/3
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
0040
MC/DC0%0/2
defined $_[0] and not ref $_[0]
defined $_[0] not ref $_[0]
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not ref $_[0]
ABresult
0X0
100
111
134 40 return 1;
135 }
136  
137 5 1;
138  
« FormBuilder/FieldSet.pm ↑ Summary