lib/HTML/FormFu/Element/Block.pm

filter: Statementstmt 82.3% 117 / 142 Branchbran 58.8% 20 / 34 Conditioncond 75.0% 6 / 8 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 95.2% 20 / 21 Podpod 40.0% 4 / 10 totaltotal 77.1% 169 / 219 SCAR 34.1
CC
24
Cov
78%
_single_plugin
72.0
string
10.1
prepare_id
6.0
CRAP 30.4
« lib/HTML/FormFu/Element/Checkboxgroup.pm ↑ Summary lib/HTML/FormFu/QueryType/Catalyst.pm »
1 158 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Element::Block;
4 158 $HTML::FormFu::Element::Block::VERSION = '2.06';
5 # ABSTRACT: Block element
6  
7 158 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 158 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 158 extends 'HTML::FormFu::Element';
11  
12 158 with 'HTML::FormFu::Role::CreateChildren',
13 'HTML::FormFu::Role::GetProcessors',
14 'HTML::FormFu::Role::ContainsElements',
15 'HTML::FormFu::Role::ContainsElementsSharedWithField',
16 'HTML::FormFu::Role::FormAndBlockMethods',
17 'HTML::FormFu::Role::FormBlockAndFieldMethods';
18  
19 158 ▶use HTML::FormFu::Constants qw( $EMPTY_STR );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 158 ▶use HTML::FormFu::Util qw( _get_elements xml_escape process_attrs );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 158 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 158 ▶use List::Util 1.45 qw( uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 158 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 158 has tag => ( is => 'rw', traits => ['Chained'] );
26 158 has nested_name => ( is => 'rw', traits => ['Chained'] );
27 158 has original_nested_name => ( is => 'rw', traits => ['Chained'] );
28 158 has auto_block_id => ( is => 'rw', traits => ['Chained'] );
29  
30 has _elements => (
31 is => 'rw',
32 270 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 158 lazy => 1,
34 isa => 'ArrayRef',
35 );
36  
37 158 __PACKAGE__->mk_output_accessors(qw( content ));
38  
39 158 *elements = \&element;
40 158 *constraints = \&constraint;
41 158 *deflators = \&deflator;
42 158 *filters = \&filter;
43 158 *inflators = \&inflator;
44 158 *validators = \&validator;
45 158 *transformers = \&transformer;
46 158 *plugins = \&plugin;
47  
48 after BUILD => sub {
49 624 ▶ my ( $self, $args ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
50  
51 624 $self->filename('block');
52 624 $self->tag('div');
53 624 $self->is_block(1);
54  
55 624 return;
56 158 };
57  
58 sub _single_plugin {
59 0 ▶ my ( $self, $arg ) = @_;
Subroutine _single_plugin: not called CC=8 SCAR=42.8
60  
61 0 ▶ if ( !ref $arg ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $arg) { } 0 0 0
elsif (ref $arg eq 'HASH') { } 0 0 0
62 0 $arg = { type => $arg };
63 }
64 elsif ( ref $arg eq 'HASH' ) {
65 0 $arg = {%$arg}; # shallow clone
66 }
67 else {
68 0 croak 'invalid args';
69 }
70  
71 0 ▶ my @names = map { ref $_ ? @$_ : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
72 0 grep {defined} ( delete $arg->{name}, delete $arg->{names} );
73  
74 0 ▶ if ( !@names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 0 0 0
75 @names = uniq
76 0 grep {defined}
77 0 map { $_->nested_name } @{ $self->get_fields };
78 }
79  
80 0 ▶ croak "no field names to add plugin to" if !@names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 0 0
81  
82 0 my $type = delete $arg->{type};
83  
84 0 my @return;
85  
86 0 for my $x (@names) {
87 0 for my $field ( @{ $self->get_fields( { nested_name => $x } ) } ) {
88 0 my $new = $field->_require_plugin( $type, $arg );
89 0 push @{ $field->_plugins }, $new;
90 0 push @return, $new;
91 }
92 }
93  
94 0 return @return;
95 }
96  
97 sub pre_process {
98 295 ▶ my ($self) = @_;
Subroutine pre_process: called CC=1 SCAR=0.0
Pod undocumented
99  
100 295 map { $_->pre_process } @{ $self->_elements };
101  
102 295 return;
103 }
104  
105 sub process {
106 341 ▶ my ($self) = @_;
Subroutine process: called CC=1 SCAR=0.0
Pod undocumented
107  
108 341 map { $_->process } @{ $self->_elements };
109  
110 341 return;
111 }
112  
113 sub post_process {
114 341 ▶ my ($self) = @_;
Subroutine post_process: called CC=1 SCAR=0.0
Pod undocumented
115  
116 341 map { $_->post_process } @{ $self->_elements };
117  
118 341 return;
119 }
120  
121 sub render_data {
122 122 ▶ my $self = shift;
Subroutine render_data: called CC=2 SCAR=7.1
Pod undocumented
123  
124 122 ▶ my $render = $self->render_data_non_recursive( { @_ ? %{ $_[0] } : () } );
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 122 122
125  
126 122 $render->{elements} = [ map { $_->render_data } @{ $self->_elements } ];
127  
128 122 return $render;
129 }
130  
131 sub render_data_non_recursive {
132 394 ▶ my ( $self, $args ) = @_;
Subroutine render_data_non_recursive: called CC=2 SCAR=6.9
Pod undocumented
133  
134 394 ▶ my $render = $self->SUPER::render_data_non_recursive(
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 257 137 394
135 { tag => $self->tag,
136 content => xml_escape( $self->content ),
137 $args ? %$args : (),
138 } );
139  
140 394 return $render;
141 }
142  
143 sub prepare_id {
144 307 ▶ my ( $self, $render ) = @_;
Subroutine prepare_id: called CC=6 SCAR=17.9
Pod documented
145  
146 307 ▶ if ( !defined $render->{attributes}{id}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $render->{"attributes"}{"id"} and defined $self->auto_block_id and length $self->auto_block_id) 3 304 307
Condition83%5/6
not defined $render->{"attributes"}{"id"} and defined $self->auto_block_id and length $self->auto_block_id
!ll&&!rl&&r
30403
not defined $render->{"attributes"}{"id"} and defined $self->auto_block_id
!ll&&!rl&&r
13033
MC/DC66%2/3
not defined $render->{"attributes"}{"id"} and defined $self->auto_block_id and length $self->auto_block_id
not defined $render->{"attributes"}{"id"} defined $self->auto_block_id length $self->auto_block_id
Truth table75%3/4
not(A) and B and C
A= not defined $render->{"attributes"}{"id"}B= defined $self->auto_block_idC= length $self->auto_block_id
ABCresult
0XX0
10X0
1100
1111
147 && defined $self->auto_block_id
148 && length $self->auto_block_id )
149 {
150 3 ▶ my $form_name
Branch: 1 branch
BranchTrueFalseTotal
defined $self->form->id ? : 1 2 3
151 = defined $self->form->id
152 ? $self->form->id
153 : $EMPTY_STR;
154  
155 3 my %string = ( f => $form_name, );
156  
157 3 my $id = $self->auto_block_id;
158 3 $id =~ s/%([f])/$string{$1}/g;
159  
160 3 ▶ if ( defined( my $count = $self->repeatable_count ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $count = $self->repeatable_count)) 2 1 3
161 2 $id =~ s/%r/$count/g;
162 }
163  
164 3 $render->{attributes}{id} = $id;
165 }
166  
167 307 return;
168 }
169  
170 sub string {
171 189 ▶ my ( $self, $args ) = @_;
Subroutine string: called CC=10 SCAR=23.2
Pod undocumented
172  
173 189 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
0189
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
174  
175 my $render
176 = exists $args->{render_data}
177 ? $args->{render_data}
178 189 ▶ : $self->render_data_non_recursive;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'render_data'} ? : 0 189 189
179  
180 # start_block template
181  
182 189 my $html = '';
183  
184 189 ▶ if ( defined $render->{tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $render->{"tag"}) 186 3 189
185 $html .= sprintf "<%s%s>",
186 $render->{tag},
187 186 process_attrs( $render->{attributes} ),
188 ;
189 }
190  
191 189 ▶ if ( defined $render->{legend} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $render->{"legend"}) 18 171 189
192 $html .=
193 sprintf "\n<legend%s>%s</legend>",
194 defined( $render->{legend_attributes} )
195 ? process_attrs( $render->{legend_attributes} )
196 : '',
197 18 ▶ $render->{legend};
Branch: 1 branch
BranchTrueFalseTotal
defined $render->{'legend_attributes'} ? : 18 0 18
198 }
199  
200 # block template
201  
202 189 $html .= "\n";
203  
204 189 ▶ if ( defined $render->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $render->{'content'}) { } 19 170 189
205 19 $html .= sprintf "%s\n", $render->{content};
206 }
207 else {
208 170 for my $elem ( @{ $self->get_elements } ) {
209  
210 # call render, so that child elements can use a different renderer
211 302 my $elem_html = $elem->render;
212  
213 # skip Blank fields
214 302 ▶ if ( length $elem_html ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $elem_html) 302 0 302
215 302 $html .= $elem_html . "\n";
216 }
217 }
218 }
219  
220 # end_block template
221  
222 189 ▶ if ( defined $render->{tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $render->{"tag"}) 186 3 189
223 186 $html .= sprintf "</%s>", $render->{tag};
224 }
225  
226 189 return $html;
227 }
228  
229 sub start {
230 2 ▶ my ($self) = @_;
Subroutine start: called CC=1 SCAR=0.0
Pod documented
231  
232 2 return $self->tt(
233 { filename => 'start_block',
234 render_data => $self->render_data_non_recursive,
235 } );
236 }
237  
238 sub end {
239 2 ▶ my ($self) = @_;
Subroutine end: called CC=1 SCAR=0.0
Pod documented
240  
241 2 return $self->tt(
242 { filename => 'end_block',
243 render_data => $self->render_data_non_recursive,
244 } );
245 }
246  
247 sub clone {
248 26 ▶ my $self = shift;
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
249  
250 26 my $clone = $self->SUPER::clone(@_);
251  
252 26 $clone->_elements( [ map { $_->clone } @{ $self->_elements } ] );
253  
254 26 map { $_->parent($clone) } @{ $clone->_elements };
255  
256 26 $clone->default_args( Clone::clone( $self->default_args ) );
257  
258 26 return $clone;
259 }
260  
261 158 __PACKAGE__->meta->make_immutable;
262  
263 158 1;
264  
265 __END__
266  
267 =pod
268  
269 =encoding UTF-8
270  
271 =head1 NAME
272  
273 HTML::FormFu::Element::Block - Block element
274  
275 =head1 VERSION
276  
277 version 2.06
278  
279 =head1 SYNOPSIS
280  
281 ---
282 elements:
283 - type: Block
284 elements:
285 - type: Text
286 name: foo
287  
288 - type: Block
289 tag: span
290 content: Whatever
291  
292 =head1 DESCRIPTION
293  
294 Block element which may contain other elements.
295  
296 =head1 METHODS
297  
298 =head2 tag
299  
300 Specifies which tag name should be used to render the block.
301  
302 Default Value: 'div'
303  
304 =head2 content
305  
306 If L</content> is set, it is used as the block's contents, and any attached
307 elements are ignored.
308  
309 =head2 content_xml
310  
311 Arguments: $string
312  
313 If you don't want the content to be XML-escaped, use the L</content_xml>
314 method instead of L</content>.
315  
316 =head2 content_loc
317  
318 Arguments: $localization_key
319  
320 To set the content to a localized string, set L</content_loc> to a key in
321 your L10N file instead of using L</content>.
322  
323 =head2 elements
324  
325 See L<HTML::FormFu/elements> for details.
326  
327 =head2 element
328  
329 See L<HTML::FormFu/element> for details.
330  
331 =head2 deflators
332  
333 See L<HTML::FormFu/deflators> for details.
334  
335 =head2 deflator
336  
337 See L<HTML::FormFu/deflator> for details.
338  
339 =head2 filters
340  
341 See L<HTML::FormFu/filters> for details.
342  
343 =head2 filter
344  
345 See L<HTML::FormFu/filter> for details.
346  
347 =head2 constraints
348  
349 See L<HTML::FormFu/constraints> for details.
350  
351 =head2 constraint
352  
353 See L<HTML::FormFu/constraint> for details.
354  
355 =head2 inflators
356  
357 See L<HTML::FormFu/inflators> for details.
358  
359 =head2 inflator
360  
361 See L<HTML::FormFu/inflator> for details.
362  
363 =head2 validators
364  
365 See L<HTML::FormFu/validators> for details.
366  
367 =head2 validator
368  
369 See L<HTML::FormFu/validator> for details.
370  
371 =head2 transformers
372  
373 See L<HTML::FormFu/transformers> for details.
374  
375 =head2 transformer
376  
377 See L<HTML::FormFu/transformer> for details.
378  
379 =head2 auto_datalist_id
380  
381 See L<HTML::FormFu/auto_datalist_id> for details.
382  
383 =head1 CSS CLASSES
384  
385 =head2 auto_id
386  
387 See L<HTML::FormFu/auto_id> for details.
388  
389 =head2 auto_block_id
390  
391 Arguments: [$string]
392  
393 If set, the Block will be given an auto-generated
394 L<id|HTML::FormFu::Element/id> attribute, if it doesn't have one already.
395  
396 The following character substitution will be performed: C<%f> will be
397 replaced by L<< $form->id|/id >>, C<%r> will be replaced by
398 L<< $block->repeatable_count|HTML::FormFu::Element::Repeatable/repeatable_count >>.
399  
400 Default Value: not defined
401  
402 Unlike most other auto_* methods, this is not an 'inherited accessor'.
403  
404 =head2 auto_label
405  
406 See L<HTML::FormFu/auto_label> for details.
407  
408 =head2 auto_error_field_class
409  
410 See L<HTML::FormFu/auto_error_field_class> for details.
411  
412 =head2 auto_error_class
413  
414 See L<HTML::FormFu/auto_error_class> for details.
415  
416 =head2 auto_error_message
417  
418 See L<HTML::FormFu/auto_error_message> for details.
419  
420 =head2 auto_constraint_class
421  
422 See L<HTML::FormFu/auto_constraint_class> for details.
423  
424 =head2 auto_inflator_class
425  
426 See L<HTML::FormFu/auto_inflator_class> for details.
427  
428 =head2 auto_validator_class
429  
430 See L<HTML::FormFu/auto_validator_class> for details.
431  
432 =head2 auto_transformer_class
433  
434 See L<HTML::FormFu/auto_transformer_class> for details.
435  
436 =head2 default_args
437  
438 See L<HTML::FormFu/default_args> for details.
439  
440 =head1 RENDERING
441  
442 =head2 start
443  
444 =head2 end
445  
446 =head1 INTROSPECTION
447  
448 =head2 get_elements
449  
450 See L<HTML::FormFu/get_elements> for details.
451  
452 =head2 get_element
453  
454 See L<HTML::FormFu/get_element> for details.
455  
456 =head2 get_all_elements
457  
458 See L<HTML::FormFu/get_all_elements> for details.
459  
460 =head2 get_fields
461  
462 See L<HTML::FormFu/get_fields> for details.
463  
464 =head2 get_field
465  
466 See L<HTML::FormFu/get_field> for details.
467  
468 =head2 get_deflators
469  
470 See L<HTML::FormFu/get_deflators> for details.
471  
472 =head2 get_deflator
473  
474 See L<HTML::FormFu/get_deflator> for details.
475  
476 =head2 get_filters
477  
478 See L<HTML::FormFu/get_filters> for details.
479  
480 =head2 get_filter
481  
482 See L<HTML::FormFu/get_filter> for details.
483  
484 =head2 get_constraints
485  
486 See L<HTML::FormFu/get_constraints> for details.
487  
488 =head2 get_constraint
489  
490 See L<HTML::FormFu/get_constraint> for details.
491  
492 =head2 get_inflators
493  
494 See L<HTML::FormFu/get_inflators> for details.
495  
496 =head2 get_inflator
497  
498 See L<HTML::FormFu/get_inflator> for details.
499  
500 =head2 get_validators
501  
502 See L<HTML::FormFu/get_validators> for details.
503  
504 =head2 get_validator
505  
506 See L<HTML::FormFu/get_validator> for details.
507  
508 =head2 get_transformers
509  
510 See L<HTML::FormFu/get_transformers> for details.
511  
512 =head2 get_transformer
513  
514 See L<HTML::FormFu/get_transformer> for details.
515  
516 =head2 get_errors
517  
518 See L<HTML::FormFu/get_errors> for details.
519  
520 =head2 clear_errors
521  
522 See L<HTML::FormFu/clear_errors> for details.
523  
524 =head1 SEE ALSO
525  
526 Base-class for L<HTML::FormFu::Element::Fieldset>.
527  
528 Is a sub-class of, and inherits methods from L<HTML::FormFu::Element>
529  
530 L<HTML::FormFu>
531  
532 =head1 REMOVED METHODS
533  
534 =head2 element_defaults
535  
536 Has been removed; use L</default_args> instead.
537  
538 =head1 AUTHOR
539  
540 Carl Franks, C<cfranks@cpan.org>
541  
542 =head1 LICENSE
543  
544 This library is free software, you can redistribute it and/or modify it under
545 the same terms as Perl itself.
546  
547 =head1 AUTHOR
548  
549 Carl Franks <cpan@fireartist.com>
550  
551 =head1 COPYRIGHT AND LICENSE
552  
553 This software is copyright (c) 2018 by Carl Franks.
554  
555 This is free software; you can redistribute it and/or modify it under
556 the same terms as the Perl 5 programming language system itself.
557  
558 =cut
« lib/HTML/FormFu/Element/Checkboxgroup.pm ↑ Summary lib/HTML/FormFu/QueryType/Catalyst.pm »