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