File Coverage

blib/lib/Rose/HTML/Form/Field/Group/OnOff.pm
Criterion Covered Total %
statement 146 159 91.8
branch 74 86 86.0
condition 15 20 75.0
subroutine 25 26 96.1
pod 5 20 25.0
total 265 311 85.2


line stmt bran cond sub pod time code
1              
2             use strict;
3 13     13   87  
  13         26  
  13         291  
4             use Carp();
5 13     13   51  
  13         22  
  13         157  
6             use Rose::HTML::Util();
7 13     13   385  
  13         24  
  13         219  
8             use base 'Rose::HTML::Form::Field::Group';
9 13     13   49  
  13         23  
  13         5324  
10             our $VERSION = '0.606';
11              
12             our $Debug = undef;
13              
14             {
15             my($self) = shift;
16              
17 560     560 0 879 $self->SUPER::resync_name();
18              
19 560         1410 # Resync item names too
20             my $name = $self->local_name;
21              
22 560         1511 foreach my $item ($self->items)
23             {
24 560         1347 $item->name($name);
25             }
26 977         2308 }
27              
28             {
29             my($self) = shift;
30              
31             if(@_)
32 2975     2975 1 7088 {
33             $self->local_name(shift);
34 2975 100       4763  
35             my $name = $self->local_name;
36 50         199  
37             # All items in the group must have the same name
38 50         117 foreach my $item ($self->items)
39             {
40             $item->name($name);
41 50         177 }
42             }
43 0         0  
44             my $name = $self->html_attr('name');
45              
46             # The name HTML attr will be an empty string if it's a required attr,
47 2975         5227 # so use length() and not defined()
48             no warnings 'uninitialized';
49             unless(length $name)
50             {
51 13     13   101 return $self->html_attr('name', $self->fq_name);
  13         27  
  13         19088  
52 2975 100       5266 }
53              
54 678         1539 return $name;
55             }
56              
57 2297         5286 {
58             my($self) = shift;
59              
60             if(@_)
61             {
62 73     73 0 691 if(@_ == 1 && ref $_[0] eq 'ARRAY')
63             {
64 73 50       229 $self->{'defaults'} = { map { $_ => 1 } @{$_[0]} };
65             }
66 73 50 33     428 elsif(defined $_[0])
    100          
67             {
68 0         0 $self->{'defaults'} = { map { $_ => 1 } @_ };
  0         0  
  0         0  
69             }
70             else
71             {
72 70         161 $self->{'defaults'} = { };
  70         357  
73             }
74              
75             $self->init_items;
76 3         13 }
77              
78             return (wantarray) ? sort keys %{$self->{'defaults'}} : [ sort keys %{$self->{'defaults'}} ];
79 73         368 }
80              
81             *default = \&defaults;
82 73 50       208 *default_value = \&defaults;
  0         0  
  73         369  
83             *default_values = \&defaults;
84              
85              
86             {
87             my($self) = shift;
88              
89 178 50   178 0 444 my $items = $self->SUPER::_args_to_items(@_);
  0         0  
90              
91             # All items in the group must have the same name
92             foreach my $item (@$items)
93 37     37   88 {
94             $item->name($self->local_name);
95 37         208 #$item->name($self->name);
96             }
97              
98 37         92 return (wantarray) ? @$items : $items;
99             }
100 87         284  
101             {
102             my($self, $value) = @_;
103              
104 37 100       180 my $group_class = $self->_item_group_class;
105              
106             # Dumb linear search for now
107             foreach my $item ($self->items)
108             {
109 70     70 0 444 if($item->isa($group_class))
110             {
111 70         215 foreach my $subitem ($item->items)
112             {
113             return $subitem if($subitem->html_attr('value') eq $value);
114 70         193 }
115             }
116 168 100       576 else
117             {
118 6         17 return $item if($item->html_attr('value') eq $value);
119             }
120 12 100       24 }
121              
122             return undef;
123             }
124              
125 162 100       327 {
126             my($self, $label) = @_;
127              
128             my $group_class = $self->_item_group_class;
129 4         17  
130             # Dumb linear search for now
131             foreach my $item ($self->items)
132             {
133             return $item if($item->isa($group_class) && $item->label eq $label);
134 3     3 0 10 }
135              
136 3         7 return undef;
137             }
138              
139 3         12 {
140             my($self, $value) = @_;
141 12 100 66     46  
142             # Dumb linear search for now
143             foreach my $item ($self->items)
144 0         0 {
145             if($item->html_attr('value') eq $value && $item->internal_value)
146             {
147             return 1;
148             }
149 19     19 0 46 }
150              
151             return 0;
152 19         68 }
153              
154 52 100 100     113 *is_checked = \&is_selected;
155              
156 8         40 {
157             my($self) = shift;
158              
159             foreach my $item ($self->items)
160 11         57 {
161             return 0 if($item->is_on);
162             }
163              
164             return 1;
165             }
166              
167 6     6 1 23 {
168             my($self) = shift;
169 6         21  
170             $self->input_value($self->internal_value, @_);
171 13 100       35 }
172              
173              
174 1         4 {
175             my($self) = shift;
176              
177             my %values;
178              
179 13     13 0 26 if(@_)
180             {
181 13         39 $self->clear();
182             $self->is_cleared(0);
183              
184 8     8 0 37 if(@_ == 1 && ref $_[0] eq 'ARRAY')
185             {
186             %values = map { $_ => 1 } @{$_[0]};
187             }
188 132     132 1 913 elsif(@_ && defined $_[0])
189             {
190 132         217 %values = map { $_ => 1 } @_;
191             }
192 132 100       331  
193             unless(%values)
194 119         476 {
195 119         760 $self->clear();
196             $self->is_cleared(0);
197 119 50 66     912 }
    100 66        
198             else
199 0         0 {
  0         0  
  0         0  
200             $self->{'values'} = \%values;
201             $self->init_items;
202             }
203 94         209  
  115         408  
204             if(my $parent = $self->parent_field)
205             {
206 119 100       274 if($parent->_is_full)
207             {
208 25         83 $parent->is_cleared(0);
209 25         141 }
210              
211             if($self->auto_invalidate_parent)
212             {
213 94         175 $parent->invalidate_value;
214 94         200 }
215             }
216             }
217 119 100       430 else
218             {
219 25 100       63 if(keys %{$self->{'values'}})
220             {
221 13         45 my $group_class = $self->_item_group_class;
222              
223             foreach my $item ($self->items)
224 25 100       171 {
225             if($item->isa($group_class))
226 8         27 {
227             foreach my $value ($item->internal_value)
228             {
229             $values{$value} = 1;
230             }
231             }
232 13 100       19 else
  13         47  
233             {
234 12         31 if($item->is_on)
235             {
236 12         37 $values{$item->html_attr('value')} = 1;
237             }
238 56 100       155 }
239             }
240 4         9 }
241             }
242 4         10  
243             return (wantarray) ? sort keys %values : [ sort keys %values ];
244             }
245              
246             {
247 52 100       99 my($self) = shift;
248              
249 16         39 my @labels;
250              
251             foreach my $value ($self->internal_value)
252             {
253             next unless(defined $value);
254             push(@labels, $self->item($value)->label);
255             }
256 132 100       693  
257             return wantarray ? @labels : \@labels;
258             }
259              
260             {
261 2     2 0 6 my($self) = shift;
262              
263 2         5 unless(@_)
264             {
265 2         9 return $self->value_labels->[0];
266             }
267 2 50       6  
268 2         14 my $value = shift;
269              
270             # Dumb linear search for now
271 2 50       16 foreach my $item ($self->items)
272             {
273             if($item->html_attr('value') eq $value)
274             {
275             return $item-label(@_) if(@_);
276 5     5 0 20 return ($item->label) ? $item->label : $value;
277             }
278 5 100       19 }
279              
280 2         15 return undef;
281             }
282              
283 3         7 #sub value_label { shift->value_labels->[0] }
284              
285              
286 3         11  
287             {
288 3 50       12 my($self) = shift;
289              
290 3 50       12 my @values;
291 3 50       10  
292             my $group_class = $self->_item_group_class;
293              
294             foreach my $item ($self->items)
295 0         0 {
296             if($item->isa($group_class))
297             {
298             push(@values, $item->internal_value) if(defined $item->internal_value);
299             }
300 5     5 1 23 else
301 8     8 0 32 {
302             push(@values, $item->value) if($item->is_on);
303 0     0 0 0 }
304             }
305              
306             @values = sort @values; # Makes tests easier to write... :-/
307 755     755 1 1106  
308             return wantarray ? @values : \@values;
309 755         971 }
310              
311 755         1616 *output_value = \&internal_value;
312              
313 755         1813  
314             {
315 2365 100       10178 my($self, $find_value) = @_;
316              
317 6 50       14 foreach my $value ($self->output_value)
318             {
319             return 1 if($value eq $find_value);
320             }
321 2359 100       4222  
322             return 0;
323             }
324              
325 755         3231 {
326             my($self) = shift;
327 755 100       1963  
328             my $values = $self->input_values_hash || {};
329              
330             if(%$values)
331             {
332 678 50   678 0 2260 my $group_class = $self->_item_group_class;
  0         0  
333              
334             foreach my $item ($self->items)
335             {
336 19     19 0 45 local $item->{'auto_invalidate_parent'} = $self->auto_invalidate_parent;
337              
338 19         45 if($item->isa($group_class))
339             {
340 28 100       86 foreach my $subitem ($item->items)
341             {
342             if(exists $values->{$subitem->html_attr('value')})
343 11         45 {
344             $subitem->input_value(1);
345             }
346             else
347             {
348 678     678 0 1020 $subitem->input_value(0);
349             }
350 678   100     1339 }
351             }
352 678 100       1914 else
    100          
353             {
354 107         278 if(exists $values->{$item->html_attr('value')})
355             {
356 107         273 $item->input_value(1);
357             }
358 401         867 else
359             {
360 401 100       1361 $item->input_value(0);
361             }
362 4         9 }
363             }
364 8 100       16 }
365             elsif(!$self->is_cleared)
366 3         8 {
367             my $defaults = $self->default_values_hash;
368              
369             foreach my $item ($self->items)
370 5         11 {
371             my $value = $item->html_attr('value');
372              
373             if(defined $value && exists $defaults->{$value})
374             {
375             $item->default_value(1);
376 397 100       828 }
377             else
378 126         308 {
379             $item->default_value(0);
380             }
381             }
382 271         619 }
383             }
384              
385             1;