Plugin/Multiselect.pm

filter: Statementstmt 100% 38 / 38 Branchbran 100% 14 / 14 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 100% 70 / 70 SCAR 23.0
CC
10
Cov
100%
validate_value
6.0
multiselect
3.0
make_option
3.0
CRAP 10.0
« Plugin/Button.pm ↑ Summary Plugin/Link.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::Multiselect;
4 # ABSTRACT: widget for multiple selections from a list
5 15 $HTML::Widget::Plugin::Multiselect::VERSION = '0.203';
6 15 ▶use parent 'HTML::Widget::Plugin::Select';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 #pod =head1 SYNOPSIS
9 #pod
10 #pod $widget_factory->multiselect({
11 #pod id => 'multiopts', # if no name attr given, defaults to id value
12 #pod size => 3,
13 #pod values => [ 'value_1', 'value_3' ],
14 #pod options => [
15 #pod [ value_1 => 'Display Name 1' ],
16 #pod [ value_2 => 'Display Name 2' ],
17 #pod [ value_3 => 'Display Name 3' ],
18 #pod ],
19 #pod });
20 #pod
21 #pod =head1 DESCRIPTION
22 #pod
23 #pod This plugin provides a select-from-list widget that allows the selection of
24 #pod multiple elements.
25 #pod
26 #pod =cut
27  
28 15 ▶use HTML::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #pod =head1 METHODS
31 #pod
32 #pod =head2 C< provided_widgets >
33 #pod
34 #pod This plugin provides the following widgets: multiselect
35 #pod
36 #pod =cut
37  
38 16 ▶sub provided_widgets { qw(multiselect) }
Subroutine provided_widgets: called CC=1 SCAR=0.0
Pod documented
39  
40 #pod =head2 C< multiselect >
41 #pod
42 #pod This method returns a multiple-selection-from-list widget. Yup.
43 #pod
44 #pod In addition to the generic L<HTML::Widget::Plugin> attributes and the
45 #pod L<HTML::Widget::Plugin::Select> attributes, the following are valid arguments:
46 #pod
47 #pod =over
48 #pod
49 #pod =item size
50 #pod
51 #pod This is the number of elements that should be visible in the widget.
52 #pod
53 #pod =back
54 #pod
55 #pod =cut
56  
57 16 ▶sub _attribute_args { qw(size) }
Subroutine _attribute_args: called CC=1 SCAR=0.0
58  
59 sub multiselect {
60 4 ▶ my ($self, $factory, $arg) = @_;
Subroutine multiselect: called CC=3 SCAR=11.0
Pod documented
61  
62 4 ▶ $arg->{attr}{name} = $arg->{attr}{id} if not defined $arg->{attr}{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg->{"attr"}{"name"} 2 2 4
63 4 $arg->{attr}{multiple} = 'multiple';
64  
65 4 ▶ if ($arg->{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"values"}) 1 3 4
66 1 $arg->{value} = delete $arg->{values};
67 }
68  
69 4 $self->build($factory, $arg);
70 }
71  
72 #pod =head2 C< make_option>
73 #pod
74 #pod This method, subclassed from the standard select widget, expects that C<$value>
75 #pod will be an array of selected values.
76 #pod
77 #pod =cut
78  
79 sub make_option {
80 12 ▶ my ($self, $factory, $value, $name, $arg) = @_;
Subroutine make_option: called CC=3 SCAR=11.0
Pod documented
81  
82 12 my $option = HTML::Element->new('option', value => $value);
83 12 $option->push_content($name);
84 $option->attr(selected => 'selected')
85 12 ▶ if $arg->{value} and grep { $_ eq $value } @{ $arg->{value} };
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"value"} and grep {$_ eq $value;} @{$$arg{"value"};} 4 8 12
Condition100%3/3
$arg->{"value"} and grep {$_ eq $value;} @{$$arg{"value"};}
!ll&&!rl&&r
444
MC/DC100%2/2
$arg->{"value"} and grep {$_ eq $value;} @{$$arg{"value"};}
$arg->{"value"} grep {$_ eq $value;} @{$$arg{"value"};}
Truth table100%3/3
A and B
A= $arg->{"value"}B= grep {$_ eq $value;} @{$$arg{"value"};}
ABresult
0X0
100
111
86  
87 12 return $option;
88 }
89  
90 #pod =head2 C< validate_value >
91 #pod
92 #pod This method checks whether the given value option is valid. It throws an
93 #pod exception if the given values are not all in the list of options.
94 #pod
95 #pod =cut
96  
97 sub validate_value {
98 4 ▶ my ($class, $values, $options) = @_;
Subroutine validate_value: called CC=6 SCAR=17.9
Pod documented
99  
100 4 ▶ $values = [ $values ] unless ref $values;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values 1 3 4
101 4 ▶ return unless grep { defined } @$values;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @$values 1 3 4
102  
103 3 for my $value (@$values) {
104 6 ▶ my $matches = grep { $value eq $_ } map { ref $_ ? $_->[0] : $_ } @$options;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 8 16 24
105 6 ▶ Carp::croak "provided value '$value' not in given options" unless $matches;
Branch: 1 branch
BranchTrueFalseTotal
unless $matches 1 5 6
106 }
107 }
108  
109 15 1;
110  
111 __END__
« Plugin/Button.pm ↑ Summary Plugin/Link.pm »