Plugin/Select.pm

filter: Statementstmt 100% 61 / 61 Branchbran 100% 26 / 26 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 100% 119 / 119 SCAR 28.9
CC
18
Cov
100%
build
10.0
validate_value
5.0
make_option
3.0
CRAP 18.0
« Plugin/Radio.pm ↑ Summary Plugin/Attrs.pm »
1 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package HTML::Widget::Plugin::Select;
4 # ABSTRACT: a widget for selection from a list
5 15 $HTML::Widget::Plugin::Select::VERSION = '0.203';
6 15 ▶use parent 'HTML::Widget::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 #pod =head1 SYNOPSIS
9 #pod
10 #pod $widget_factory->select({
11 #pod id => 'the-selector', # if no name attr given, defaults to id value
12 #pod value => 10,
13 #pod options => [
14 #pod [ 0 => "Zero" ],
15 #pod [ 5 => "Five" ],
16 #pod [ 10 => "Ten" ],
17 #pod ],
18 #pod });
19 #pod
20 #pod =head1 DESCRIPTION
21 #pod
22 #pod This plugin provides a select-from-list widget.
23 #pod
24 #pod The C<default_classes> attribute may be used to add a default class to every
25 #pod produced input. This class cannot be overridden.
26 #pod
27 #pod my $plugin = HTML::Widget::Factory::Input->new({
28 #pod default_classes => [ qw(foo bar) ],
29 #pod });
30 #pod
31 #pod =head1 METHODS
32 #pod
33 #pod =head2 C< provided_widgets >
34 #pod
35 #pod This plugin provides the following widgets: select
36 #pod
37 #pod =cut
38  
39 17 ▶sub provided_widgets { qw(select) }
Subroutine provided_widgets: called CC=1 SCAR=0.0
Pod documented
40  
41 #pod =head2 C< select >
42 #pod
43 #pod This method returns a select-from-list widget.
44 #pod
45 #pod In addition to the generic L<HTML::Widget::Plugin> attributes, the following
46 #pod are valid arguments:
47 #pod
48 #pod =over
49 #pod
50 #pod =item disabled
51 #pod
52 #pod If true, this option indicates that the select widget can't be changed by the
53 #pod user.
54 #pod
55 #pod =item ignore_invalid
56 #pod
57 #pod If this is given and true, an invalid value is ignored instead of throwing an
58 #pod exception.
59 #pod
60 #pod =item options
61 #pod
62 #pod This may be an arrayref of arrayrefs, each containing a value/name pair, or it
63 #pod may be a hashref of values and names.
64 #pod
65 #pod Use the array form if you need multiple entries for a single value or if order
66 #pod is important.
67 #pod
68 #pod =item value
69 #pod
70 #pod If this argument is given, the option with this value will be pre-selected in
71 #pod the widget's initial state.
72 #pod
73 #pod An exception will be thrown if more or less than one of the provided options
74 #pod has this value.
75 #pod
76 #pod =back
77 #pod
78 #pod =cut
79  
80 15 ▶use HTML::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 33 ▶sub _attribute_args { qw(disabled) }
Subroutine _attribute_args: called CC=1 SCAR=0.0
83 82 ▶sub _boolean_args { qw(disabled) }
Subroutine _boolean_args: called CC=1 SCAR=0.0
84  
85 sub select { ## no critic Builtin
86 11 ▶ my ($self, $factory, $arg) = @_;
Subroutine select: called CC=1 SCAR=0.0
Pod documented
87  
88 11 $self->build($factory, $arg);
89 }
90  
91 #pod =head2 C< build >
92 #pod
93 #pod my $widget = $class->build($factory, \%arg)
94 #pod
95 #pod This method does the actual construction of the widget based on the args set up
96 #pod in the exported widget-constructing call. It's here for subclasses to exploit.
97 #pod
98 #pod =cut
99  
100 sub build {
101 15 ▶ my ($self, $factory, $arg) = @_;
Subroutine build: called CC=10 SCAR=23.0
Pod documented
102 15 ▶ $arg->{attr}{name} = $arg->{attr}{id} unless $arg->{attr}{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->{"attr"}{"name"} 5 10 15
103  
104 15 my $widget = HTML::Element->new('select');
105  
106 15 my @options;
107 15 ▶ if (ref $arg->{options} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg->{'options'} eq 'HASH') { } 1 14 15
108 1 @options = map { [ $_, $arg->{options}{$_} ] } keys %{ $arg->{options} };
109 } else {
110 14 @options = @{ $arg->{options} };
111 Carp::croak "undefined value passed to select widget"
112 14 ▶ if grep { not(defined $_) or ref $_ and not defined $_->[0] } @options;
Branch: 2 branches
BranchTrueFalseTotal
if grep {not defined $_ or ref $_ and not defined $_->[0];} @options 2 12 14
not defined $_ or ref $_ and not defined $_->[0] 55 1 56
Condition100%6/6
!defined($_) || ref $_ && !defined($_->[0])
l!l&&r!l&&!r
1154
ref $_ and not defined $_->[0]
!ll&&!rl&&r
36181
MC/DC100%3/3
!defined($_) || ref $_ && !defined($_->[0])
!defined($_) ref $_ not defined $_->[0]
Truth table100%4/4
not(A) || B and not(C)
A= !defined($_)B= ref $_C= not defined $_->[0]
ABCresult
1XX1
0111
00X0
0100
113 }
114  
115 13 ▶ $self->validate_value($arg->{value}, \@options) unless $arg->{ignore_invalid};
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->{"ignore_invalid"} 11 2 13
116  
117 10 for my $entry (@options) {
118 44 ▶ my ($value, $name) = (ref $entry) ? @$entry : ($entry) x 2;
Branch: 1 branch
BranchTrueFalseTotal
ref $entry ? : 15 29 44
119 44 my $option = $self->make_option($factory, $value, $name, $arg);
120 44 $widget->push_content($option);
121 }
122  
123 10 $widget->attr($_ => $arg->{attr}{$_}) for keys %{ $arg->{attr} };
124 10 return $widget->as_XML;
125 }
126  
127 #pod =head2 C< make_option >
128 #pod
129 #pod my $option = $class->make_option($factory, $value, $name, $arg);
130 #pod
131 #pod This method constructs the HTML::Element option element that will represent one
132 #pod of the options that may be put into the select box. This is here for
133 #pod subclasses to exploit.
134 #pod
135 #pod =cut
136  
137 sub make_option {
138 32 ▶ my ($self, $factory, $value, $name, $arg) = @_;
Subroutine make_option: called CC=3 SCAR=11.0
Pod documented
139  
140 32 my $option = HTML::Element->new('option', value => $value);
141 32 $option->push_content($name);
142 $option->attr(selected => 'selected')
143 32 ▶ if defined $arg->{value} and $arg->{value} eq $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg->{"value"} and $arg->{"value"} eq $value 4 28 32
Condition100%3/3
defined $arg->{"value"} and $arg->{"value"} eq $value
!ll&&!rl&&r
2174
MC/DC100%2/2
defined $arg->{"value"} and $arg->{"value"} eq $value
defined $arg->{"value"} $arg->{"value"} eq $value
Truth table100%3/3
A and B
A= defined $arg->{"value"}B= $arg->{"value"} eq $value
ABresult
0X0
100
111
144  
145 32 return $option;
146 }
147  
148 #pod =head2 C< validate_value >
149 #pod
150 #pod This method checks whether the given value option is valid. See C<L</select>>
151 #pod for an explanation of its default rules.
152 #pod
153 #pod =cut
154  
155 sub validate_value {
156 7 ▶ my ($class, $value, $options) = @_;
Subroutine validate_value: called CC=5 SCAR=16.1
Pod documented
157  
158 7 ▶ my @options = map { ref $_ ? $_->[0] : $_ } @$options;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 11 21 32
159 # maybe this should be configurable?
160 7 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 4 3 7
161 4 my $matches = grep { $value eq $_ } @options;
162  
163 4 ▶ if (not $matches) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $matches) { } 1 3 4
elsif ($matches > 1) { } 1 2 3
164 Carp::croak "provided value '$value' not in given options: "
165 1 . join(q{ }, map { "'$_'" } @options);
166 } elsif ($matches > 1) {
167 1 Carp::croak "provided value '$matches' matches more than one option";
168 }
169 }
170 }
171  
172 sub rewrite_arg {
173 15 ▶ my ($self, $arg, @rest) = @_;
Subroutine rewrite_arg: called CC=3 SCAR=11.0
Pod documented
174  
175 15 $arg = $self->SUPER::rewrite_arg($arg, @rest);
176  
177 15 ▶ if ($self->{default_classes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"default_classes"}) 2 13 15
178 2 my $class = join q{ }, @{ $self->{default_classes} };
179 $arg->{attr}{class} = defined $arg->{attr}{class}
180 2 ▶ ? "$class $arg->{attr}{class}"
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->{'attr'}{'class'} ? : 1 1 2
181 : $class;
182 }
183  
184 15 return $arg;
185 }
186  
187  
188 15 1;
189  
190 __END__
« Plugin/Radio.pm ↑ Summary Plugin/Attrs.pm »