Plugin/Radio.pm

filter: Statementstmt 98.1% 52 / 53 Branchbran 88.4% 23 / 26 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 87.3% 97 / 111 CC 20 SCAR 23.9
CC
20
Cov
90%
radio
17.1
validate_value
5.0
BEGIN
1.0
CRAP 10.9
↑ 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::Radio;
4 # ABSTRACT: a widget for sets of radio buttons
5 15 $HTML::Widget::Plugin::Radio::VERSION = '0.204';
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->radio({
11 #pod name => 'radio',
12 #pod value => 'value_1',
13 #pod options => [
14 #pod [ value_1 => "Description 1" ],
15 #pod [ value_2 => "Description 2" ],
16 #pod [ value_2 => "Description 2", 'optional-elem-id' ],
17 #pod ],
18 #pod });
19 #pod
20 #pod This will emit roughly:
21 #pod
22 #pod <input type='radio' name='radio' value='value_1' id='radio-value_1'
23 #pod checked='checked'></input>
24 #pod <label for='radio-value_1'>Description 2</label>
25 #pod
26 #pod <input type='radio' name='radio' value='value_2' id='radio-value_2'></input>
27 #pod <label for='radio-value_2'>Description 2</label>
28 #pod
29 #pod <input type='radio' name='radio' value='value_3'
30 #pod id='optional-elem-id'></input>
31 #pod <label for='optional-elem-id'>Description 2</label>
32 #pod
33 #pod =head1 DESCRIPTION
34 #pod
35 #pod This plugin provides a radio button-set widget
36 #pod
37 #pod =cut
38  
39 15 ▶use HTML::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 #pod =head1 METHODS
42 #pod
43 #pod =head2 C< provided_widgets >
44 #pod
45 #pod This plugin provides the following widgets: radio
46 #pod
47 #pod =cut
48  
49 16 ▶sub provided_widgets { qw(radio) }
Subroutine provided_widgets: called CC=1 SCAR=0.0
Pod documented
50  
51 #pod =head2 C< radio >
52 #pod
53 #pod This method returns a set of radio buttons.
54 #pod
55 #pod In addition to the generic L<HTML::Widget::Plugin> attributes, the following
56 #pod are valid arguments:
57 #pod
58 #pod =over
59 #pod
60 #pod =item disabled
61 #pod
62 #pod If true, this option indicates that the select widget can't be changed by the
63 #pod user.
64 #pod
65 #pod =item ignore_invalid
66 #pod
67 #pod If this is given and true, an invalid value is ignored instead of throwing an
68 #pod exception.
69 #pod
70 #pod =item options
71 #pod
72 #pod This option must be a reference to an array of allowed values, each of which
73 #pod will get its own radio button.
74 #pod
75 #pod =item value
76 #pod
77 #pod If this argument is given, the option with this value will be pre-selected in
78 #pod the widget's initial state.
79 #pod
80 #pod An exception will be thrown if more or less than one of the provided options
81 #pod has this value.
82 #pod
83 #pod =back
84 #pod
85 #pod =cut
86  
87 16 ▶sub _attribute_args { qw(disabled) }
Subroutine _attribute_args: called CC=1 SCAR=0.0
88 32 ▶sub _boolean_args { qw(disabled) }
Subroutine _boolean_args: called CC=1 SCAR=0.0
89  
90 8 ▶sub radio {
Subroutine radio: called CC=16 SCAR=28.4
Pod documented
91 8 my ($self, $factory, $arg) = @_;
92  
93 8 my @widgets;
94  
95 $self->validate_value($arg->{value}, $arg->{options})
96 8 ▶ unless $arg->{ignore_invalid};
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->{"ignore_invalid"} 7 1 8
97  
98 6 ▶ if (my $id_attr = delete $arg->{attr}{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id_attr = delete $arg->{"attr"}{"id"}) 3 3 6
99 3 Carp::cluck "id may not be used as a widget-level attribute for radio";
100 3 ▶ $arg->{attr}{name} = $id_attr if not defined $arg->{attr}{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg->{"attr"}{"name"} 2 1 3
101 }
102  
103 6 for my $option (@{ $arg->{options} }) {
104 18 ▶ my ($value, $text, $id) = (ref $option) ? (@$option) : (($option) x 2);
Branch: 1 branch
BranchTrueFalseTotal
ref $option ? : 12 6 18
105  
106 18 my $widget = HTML::Element->new('input', type => 'radio');
107 18 $widget->attr($_ => $arg->{attr}{$_}) for keys %{ $arg->{attr} };
108  
109 $id = "$arg->{attr}{name}-$value"
110 18 ▶ if ! defined $id and defined $arg->{attr}{name};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $id and defined $arg->{"attr"}{"name"} 15 3 18
Condition66%2/3
not defined $id and defined $arg->{"attr"}{"name"}
!ll&&!rl&&r
3015
MC/DC50%1/2
not defined $id and defined $arg->{"attr"}{"name"}
not defined $id defined $arg->{"attr"}{"name"}
Truth table66%2/3
not(A) and B
A= not defined $idB= defined $arg->{"attr"}{"name"}
ABresult
0X0
100
111
111  
112 18 ▶ $widget->attr(id => $id) if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 18 0 18
113  
114 18 $widget->attr(value => $value);
115  
116 $widget->attr(checked => 'checked')
117 18 ▶ if defined $arg->{value} and $arg->{value} eq $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg->{"value"} and $arg->{"value"} eq $value 4 14 18
Condition100%3/3
defined $arg->{"value"} and $arg->{"value"} eq $value
!ll&&!rl&&r
3114
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
118  
119 18 push @widgets, $widget;
120  
121 18 my $text_elem = HTML::Element->new('~literal', text => $text);
122 18 ▶ if (! $arg->{parts} and defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $arg->{'parts'} and defined $id) { } 18 0 18
Condition33%1/3
not $arg->{"parts"} and defined $id
!ll&&!rl&&r
0018
MC/DC0%0/2
not $arg->{"parts"} and defined $id
not $arg->{"parts"} defined $id
Truth table33%1/3
not(A) and B
A= not $arg->{"parts"}B= defined $id
ABresult
0X0
100
111
123 18 my $label = HTML::Element->new(label => (for => $id));
124 18 $label->push_content($text_elem);
125 18 push @widgets, $label;
126 } else {
127 0 push @widgets, $text_elem;
128 }
129 }
130  
131 # XXX document
132 6 ▶ return @widgets if wantarray and $arg->{parts};
Branch: 1 branch
BranchTrueFalseTotal
if wantarray and $arg->{"parts"} 0 6 6
Condition33%1/3
wantarray and $arg->{"parts"}
!ll&&!rl&&r
600
MC/DC0%0/2
wantarray and $arg->{"parts"}
wantarray $arg->{"parts"}
Truth table33%1/3
A and B
A= wantarrayB= $arg->{"parts"}
ABresult
0X0
100
111
133  
134 6 return join q{}, map { $_->as_XML } @widgets;
135 }
136  
137 #pod =head2 C< validate_value >
138 #pod
139 #pod This method checks whether the given value option is valid. See C<L</radio>>
140 #pod for an explanation of its default rules.
141 #pod
142 #pod =cut
143  
144 7 ▶sub validate_value {
Subroutine validate_value: called CC=5 SCAR=16.1
Pod documented
145 7 my ($class, $value, $options) = @_;
146  
147 7 ▶ my @options = map { ref $_ ? $_->[0] : $_ } @$options;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 15 6 21
148  
149 7 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 6 1 7
150 6 my $matches = grep { $value eq $_ } @options;
151  
152 6 ▶ if (not $matches) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $matches) { } 1 5 6
elsif ($matches > 1) { } 1 4 5
153 Carp::croak "provided value '$value' not in given options: "
154 1 . join(q{ }, map { "'$_'" } @options);
155 } elsif ($matches > 1) {
156 1 Carp::croak "provided value '$value' matches more than one option";
157 }
158 }
159 }
160  
161 15 1;
162  
163 __END__
↑ Summary Plugin/Attrs.pm »