lib/HTML/FormFu/Element/ComboBox.pm

filter: Statementstmt 95.9% 141 / 147 Branchbran 78.1% 25 / 32 Conditioncond 75.0% 9 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 25 / 25 Podpod 12.5% 1 / 8 totaltotal 88.7% 206 / 232 SCAR 31.5
CC
23
Cov
92%
_combobox_defaults
7.1
options
7.0
process_input
5.0
CRAP 23.3
« lib/HTML/FormFu/Element.pm ↑ Summary lib/HTML/FormFu/Role/Element/MultiElement.pm »
1 package HTML::FormFu::Element::ComboBox;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 our $VERSION = '2.05'; # VERSION
5  
6 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use MooseX::Attribute::FormFuChained;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 extends 'HTML::FormFu::Element::Multi';
9  
10 5 with 'HTML::FormFu::Role::Element::ProcessOptionsFromModel';
11  
12 5 ▶use HTML::FormFu::Util qw( _filter_components _parse_args );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use List::Util 1.33 qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Moose::Util qw( apply_all_roles );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 our @DEFER_TO_SELECT = qw(
17 empty_first
18 empty_first_label
19 values
20 value_range
21 );
22  
23 5 for my $name (@DEFER_TO_SELECT) {
24 20 has $name => ( is => 'rw', traits => ['FormFuChained'] );
25 }
26  
27 4 ▶has select => ( is => 'rw', traits => ['FormFuChained'], default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
28 4 ▶has text => ( is => 'rw', traits => ['FormFuChained'], default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
29  
30 5 *default = \&value;
31  
32 ## build get_Xs methods
33 5 for my $method ( qw(
34 deflator filter
35 constraint inflator
36 validator transformer
37 ) )
38 {
39 my $sub = sub {
40 52 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
41 52 my %args = _parse_args(@_);
42 52 my $get_method = "get_${method}s";
43  
44 52 my $accessor = "_${method}s";
45 52 my @x = @{ $self->$accessor };
46 52 push @x, map { @{ $_->$get_method(@_) } } @{ $self->_elements };
47  
48 52 return _filter_components( \%args, \@x );
49 30 };
50  
51 30 my $name = __PACKAGE__ . "::get_${method}s";
52  
53 ## no critic (ProhibitNoStrict);
54 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 30 *{$name} = $sub;
57 }
58  
59 after BUILD => sub {
60 4 ▶ my ( $self, $args ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
61  
62 4 $self->multi_value(1);
63 4 $self->empty_first(1);
64  
65 4 return;
66 5 };
67  
68 sub options {
69 1 ▶ my ( $self, @args ) = @_;
Subroutine options: called CC=4 SCAR=19.4
Pod documented
70  
71 1 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 1 0 1
72 1 ▶ $self->{options} = @args == 1 ? $args[0] : \@args;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 1 0 1
73  
74 1 return $self;
75 }
76 else {
77  
78 # we're being called as a getter!
79 # are the child elements made yet?
80  
81 0 ▶ if ( !@{ $self->_elements } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->_elements;}) 0 0 0
82  
83 # need to build the children, so we can return the select options
84 0 $self->_add_elements;
85 }
86  
87 0 return $self->_elements->[0]->options;
88 }
89 }
90  
91 sub value {
92 47 ▶ my ( $self, $value ) = @_;
Subroutine value: called CC=3 SCAR=11.0
Pod undocumented
93  
94 47 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 2 45 47
95 2 $self->{value} = $value;
96  
97 # if we're already built - i.e. process() has been called,
98 # call default() on our children
99  
100 2 ▶ if ( @{ $self->_elements } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_elements;}) 2 0 2
101 2 $self->_combobox_defaults;
102  
103 2 $self->_elements->[0]->default( $self->select->{default} );
104 2 $self->_elements->[1]->default( $self->text->{default} );
105 }
106  
107 2 return $self;
108 }
109  
110 45 return $self->{value};
111 }
112  
113 sub _add_elements {
114 15 ▶ my ($self) = @_;
Subroutine _add_elements: called CC=1 SCAR=0.0
115  
116 15 $self->_elements( [] );
117  
118 15 $self->_add_select;
119 15 $self->_add_text;
120  
121 15 $self->_combobox_defaults;
122  
123 15 return;
124 }
125  
126 sub _combobox_defaults {
127 17 ▶ my ($self) = @_;
Subroutine _combobox_defaults: called CC=7 SCAR=19.6
128  
129 17 ▶ if ( defined( my $default = $self->default ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $default = $self->default)) 7 10 17
130  
131 7 ▶ if ( !$self->form->submitted || $self->render_processed_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->form->submitted or $self->render_processed_value) 5 2 7
Condition66%2/3
not $self->form->submitted or $self->render_processed_value
l!l&&r!l&&!r
502
MC/DC50%1/2
not $self->form->submitted or $self->render_processed_value
not $self->form->submitted $self->render_processed_value
Truth table66%2/3
not(A) or B
A= not $self->form->submittedB= $self->render_processed_value
ABresult
1X1
011
000
132 5 for my $deflator ( @{ $self->_deflators } ) {
133 0 $default = $deflator->process($default);
134 }
135 }
136  
137 7 my $select_options = $self->_elements->[0]->options;
138  
139 7 ▶ if ( $default ne ''
Branch: 1 branch
BranchTrueFalseTotal
if ($default ne '' and &any(sub { $_->{'value'} eq $default; } , @$select_options)) { } 2 5 7
Condition66%2/3
$default ne "" and &any(sub {
!ll&&!rl&&r
052
MC/DC50%1/2
$default ne "" and &any(sub { $_->{"value"} eq $default; } , @$select_options)
$default ne "" &any(sub { $_->{"value"} eq $default; } , @$select_options)
Truth table66%2/3
A and B
A= $default ne ""B= &any(sub { $_->{"value"} eq $default; } , @$select_options)
ABresult
0X0
100
111
140 24 ▶ && any { $_->{value} eq $default } @$select_options )
Subroutine __ANON__: called CC=1 SCAR=0.0
141 {
142 2 $self->select->{default} = $default;
143 2 $self->text->{default} = undef;
144 }
145 else {
146 5 $self->select->{default} = undef;
147 5 $self->text->{default} = $default;
148 }
149 }
150  
151 17 return;
152 }
153  
154 sub _add_select {
155 15 ▶ my ($self) = @_;
Subroutine _add_select: called CC=5 SCAR=16.1
156  
157 15 my $select = $self->select;
158  
159 15 my $select_name = _build_field_name( $self, 'select' );
160  
161 15 my $select_element = $self->element( {
162 type => 'Select',
163 name => $select_name,
164 } );
165  
166 15 apply_all_roles( $select_element,
167 'HTML::FormFu::Role::Element::MultiElement' );
168  
169 15 for my $method (@DEFER_TO_SELECT) {
170 60 ▶ if ( defined( my $value = $self->$method ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $value = $self->$method)) 27 33 60
171 27 $select_element->$method($value);
172 }
173 }
174  
175 15 ▶ if ( !@{ $select_element->options } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$select_element->options;}) 3 12 15
176  
177 # we need to access the hashkey directly,
178 # otherwise we'll have a loop
179 3 $select_element->options( $self->{options} );
180 }
181  
182 15 ▶ if ( defined( my $default = $select->{default} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $default = $select->{"default"})) 1 14 15
183 1 $select_element->default($default);
184 }
185  
186 15 return;
187 }
188  
189 sub _add_text {
190 15 ▶ my ($self) = @_;
Subroutine _add_text: called CC=2 SCAR=6.9
191  
192 15 my $text = $self->text;
193  
194 15 my $text_name = _build_field_name( $self, 'text' );
195  
196 15 my $text_element = $self->element( {
197 type => 'Text',
198 name => $text_name,
199 } );
200  
201 15 apply_all_roles( $text_element,
202 'HTML::FormFu::Role::Element::MultiElement' );
203  
204 15 ▶ if ( defined( my $default = $text->{default} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $default = $text->{"default"})) 4 11 15
205 4 $text_element->default($default);
206 }
207  
208 15 return;
209 }
210  
211 sub get_select_field_nested_name {
212 11 ▶ my ($self) = @_;
Subroutine get_select_field_nested_name: called CC=1 SCAR=0.0
Pod undocumented
213  
214 11 my $select_name = _build_field_name( $self, 'select' );
215  
216 11 return $self->get_element( { name => $select_name } )->nested_name;
217 }
218  
219 sub get_text_field_nested_name {
220 11 ▶ my ($self) = @_;
Subroutine get_text_field_nested_name: called CC=1 SCAR=0.0
Pod undocumented
221  
222 11 my $text_name = _build_field_name( $self, 'text' );
223  
224 11 return $self->get_element( { name => $text_name } )->nested_name;
225 }
226  
227 sub _build_field_name {
228 52 ▶ my ( $self, $type ) = @_;
Subroutine _build_field_name: called CC=2 SCAR=7.1
229  
230 52 my $options = $self->$type;
231 52 my $name;
232  
233 52 ▶ if ( defined( my $default_name = $options->{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $default_name = $options->{'name'})) { } 0 52 52
234 0 $name = $default_name;
235 }
236 else {
237 52 $name = sprintf "%s_%s", $self->name, $type;
238 }
239  
240 52 return $name;
241 }
242  
243 sub process {
244 15 ▶ my ( $self, @args ) = @_;
Subroutine process: called CC=1 SCAR=0.0
Pod undocumented
245  
246 15 $self->_process_options_from_model;
247  
248 15 $self->_add_elements;
249  
250 15 return $self->SUPER::process(@args);
251 }
252  
253 sub process_input {
254 11 ▶ my ( $self, $input ) = @_;
Subroutine process_input: called CC=5 SCAR=16.1
Pod undocumented
255  
256 11 my $select_name = $self->get_select_field_nested_name;
257 11 my $text_name = $self->get_text_field_nested_name;
258  
259 11 my $select_value = $self->get_nested_hash_value( $input, $select_name );
260 11 my $text_value = $self->get_nested_hash_value( $input, $text_name );
261  
262 11 ▶ if ( defined $text_value && length $text_value ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $text_value and length $text_value) { } 4 7 11
elsif (defined $select_value and length $select_value) { } 6 1 7
Condition100%3/3
defined $text_value and length $text_value
!ll&&!rl&&r
344
MC/DC100%2/2
defined $text_value and length $text_value
defined $text_value length $text_value
Truth table100%3/3
A and B
A= defined $text_valueB= length $text_value
ABresult
0X0
100
111
263 4 $self->set_nested_hash_value( $input, $self->nested_name, $text_value,
264 );
265 }
266 ▶ elsif ( defined $select_value && length $select_value ) {
Condition66%2/3
defined $select_value and length $select_value
!ll&&!rl&&r
016
MC/DC50%1/2
defined $select_value and length $select_value
defined $select_value length $select_value
Truth table66%2/3
A and B
A= defined $select_valueB= length $select_value
ABresult
0X0
100
111
267 6 $self->set_nested_hash_value( $input, $self->nested_name, $select_value,
268 );
269 }
270  
271 11 return $self->SUPER::process_input($input);
272 }
273  
274 sub render_data {
275 6 ▶ return shift->render_data_non_recursive(@_);
Subroutine render_data: called CC=1 SCAR=0.0
Pod undocumented
276 }
277  
278 sub render_data_non_recursive {
279 6 ▶ my ( $self, $args ) = @_;
Subroutine render_data_non_recursive: called CC=2 SCAR=7.0
Pod undocumented
280  
281 my $render = $self->SUPER::render_data_non_recursive( {
282 6 ▶ elements => [ map { $_->render_data } @{ $self->_elements } ],
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 0 6 6
283 $args ? %$args : (),
284 } );
285  
286 6 return $render;
287 }
288  
289 5 __PACKAGE__->meta->make_immutable;
290  
291 5 1;
292  
293 __END__
294  
295 =head1 NAME
296  
297 HTML::FormFu::Element::ComboBox - Select / Text hybrid
298  
299 =head1 VERSION
300  
301 version 2.05
302  
303 =head1 SYNOPSIS
304  
305 ---
306 elements:
307 - type: ComboBox
308 name: answer
309 label: 'Select yes or no, or write an alternative:'
310 values:
311 - yes
312 - no
313  
314  
315 =head1 DESCRIPTION
316  
317 Creates a L<multi|HTML::FormFu::Element::Multi> element containing a Select
318 field and a Text field.
319  
320 A ComboBox element named C<foo> would result in a Select menu named
321 C<foo_select> and a Text field named C<foo_text>. The names can instead be
322 overridden by the C<name> value in L</select> and L</text>.
323  
324 If a value is submitted for the Text field, this will be used in preference
325 to any submitted value for the Select menu.
326  
327 You can access the submitted value by using the ComboBox's name:
328  
329 my $value = $form->param_value('foo');
330  
331 =head1 METHODS
332  
333 =head2 default
334  
335 If the value matches one of the Select menu's options, that options will be
336 selected. Otherwise, the Text field will use the value as its default.
337  
338 =head2 options
339  
340 See L<HTML::FormFu::Role::Element::Group/options> for details.
341  
342 =head2 values
343  
344 See L<HTML::FormFu::Role::Element::Group/values> for details.
345  
346 =head2 value_range
347  
348 See L<HTML::FormFu::Role::Element::Group/value_range> for details.
349  
350 =head2 empty_first
351  
352 See L<HTML::FormFu::Role::Element::Group/empty_first> for details.
353  
354 =head2 empty_first_label
355  
356 See L<HTML::FormFu::Role::Element::Group/empty_first_label> for details.
357  
358 =head2 select
359  
360 Arguments: \%setting
361  
362 Set values effecting the Select menu. Known keys are:
363  
364 =head3 name
365  
366 Override the auto-generated name of the select menu.
367  
368 =head2 text
369  
370 Arguments: \%setting
371  
372 Set values effecting the Text field. Known keys are:
373  
374 =head3 name
375  
376 Override the auto-generated name of the select menu.
377  
378 =head1 CAVEATS
379  
380 Although this element inherits from L<HTML::FormFu::Element::Block>, its
381 behaviour for the methods
382 L<filterE<sol>filters|HTML::FormFu/filters>,
383 L<constraintE<sol>constraints|HTML::FormFu/constraints>,
384 L<inflatorE<sol>inflators|HTML::FormFu/inflators>,
385 L<validatorE<sol>validators|HTML::FormFu/validators> and
386 L<transformerE<sol>transformers|HTML::FormFu/transformers> is more like that of
387 a L<field element|HTML::FormFu::Role::Element::Field>, meaning all processors are
388 added directly to the date element, not to its child elements.
389  
390 This element's L<get_elements|HTML::FormFu/get_elements> and
391 L<get_all_elements|HTML::FormFu/get_all_elements> are inherited from
392 L<HTML::FormFu::Element::Block>, and so have the same behaviour. However, it
393 overrides the C<get_fields|HTML::FormFu/get_fields> method, such that it
394 returns both itself and its child elements.
395  
396 =head1 SEE ALSO
397  
398 Is a sub-class of, and inherits methods from
399 L<HTML::FormFu::Element::Multi>,
400 L<HTML::FormFu::Element::Block>,
401 L<HTML::FormFu::Element>
402  
403 L<HTML::FormFu>
404  
405 =head1 AUTHOR
406  
407 Carl Franks, C<cfranks@cpan.org>
408  
409 =head1 LICENSE
410  
411 This library is free software, you can redistribute it and/or modify it under
412 the same terms as Perl itself.
413  
414 =cut
« lib/HTML/FormFu/Element.pm ↑ Summary lib/HTML/FormFu/Role/Element/MultiElement.pm »