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 CC 24 SCAR 28.2
CC
24
Cov
78%
_single_plugin
72.0
string
10.1
prepare_id
6.0
CRAP 16.8
« lib/HTML/FormFu/Role/Element/Field.pm ↑ Summary lib/HTML/FormFu/Role/Element/ProcessOptionsFromModel.pm »
1 170 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Element::Block;
4 170 $HTML::FormFu::Element::Block::VERSION = '2.09';
5 # ABSTRACT: Block element
6  
7 170 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 170 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 170 extends 'HTML::FormFu::Element';
11  
12 170 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 170 ▶use HTML::FormFu::Constants qw( $EMPTY_STR );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 170 ▶use HTML::FormFu::Util qw( _get_elements xml_escape process_attrs );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 170 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 170 ▶use List::Util 1.45 qw( uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 170 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 170 has tag => ( is => 'rw', traits => ['Chained'] );
26 170 has nested_name => ( is => 'rw', traits => ['Chained'] );
27 170 has original_nested_name => ( is => 'rw', traits => ['Chained'] );
28 170 has auto_block_id => ( is => 'rw', traits => ['Chained'] );
29  
30 has _elements => (
31 is => 'rw',
32 278 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 170 lazy => 1,
34 isa => 'ArrayRef',
35 );
36  
37 170 __PACKAGE__->mk_output_accessors(qw( content ));
38  
39 170 *elements = \&element;
40 170 *constraints = \&constraint;
41 170 *deflators = \&deflator;
42 170 *filters = \&filter;
43 170 *inflators = \&inflator;
44 170 *validators = \&validator;
45 170 *transformers = \&transformer;
46 170 *plugins = \&plugin;
47  
48 1002 ▶after BUILD => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
49 1002 my ( $self, $args ) = @_;
50  
51 1002 $self->filename('block');
52 1002 $self->tag('div');
53 1002 $self->is_block(1);
54  
55 1002 return;
56 170 };
57  
58 0 ▶sub _single_plugin {
Subroutine _single_plugin: not called CC=8 SCAR=42.8
59 0 my ( $self, $arg ) = @_;
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 302 ▶sub pre_process {
Subroutine pre_process: called CC=1 SCAR=0.0
Pod undocumented
98 302 my ($self) = @_;
99  
100 302 map { $_->pre_process } @{ $self->_elements };
101  
102 302 return;
103 }
104  
105 509 ▶sub process {
Subroutine process: called CC=1 SCAR=0.0
Pod undocumented
106 509 my ($self) = @_;
107  
108 509 map { $_->process } @{ $self->_elements };
109  
110 509 return;
111 }
112  
113 509 ▶sub post_process {
Subroutine post_process: called CC=1 SCAR=0.0
Pod undocumented
114 509 my ($self) = @_;
115  
116 509 map { $_->post_process } @{ $self->_elements };
117  
118 509 return;
119 }
120  
121 122 ▶sub render_data {
Subroutine render_data: called CC=2 SCAR=7.1
Pod undocumented
122 122 my $self = shift;
123  
124 0 ▶ 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 374 ▶sub render_data_non_recursive {
Subroutine render_data_non_recursive: called CC=2 SCAR=6.9
Pod undocumented
132 374 my ( $self, $args ) = @_;
133  
134 374 ▶ my $render = $self->SUPER::render_data_non_recursive(
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 259 115 374
135 { tag => $self->tag,
136 content => xml_escape( $self->content ),
137 $args ? %$args : (),
138 } );
139  
140 374 return $render;
141 }
142  
143 285 ▶sub prepare_id {
Subroutine prepare_id: called CC=6 SCAR=17.9
Pod documented
144 285 my ( $self, $render ) = @_;
145  
146 285 ▶ 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 282 285
Condition83%5/6
not defined $render->{"attributes"}{"id"} and defined $self->auto_block_id and length $self->auto_block_id
!ll&&!rl&&r
28203
not defined $render->{"attributes"}{"id"} and defined $self->auto_block_id
!ll&&!rl&&r
12813
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 285 return;
168 }
169  
170 189 ▶sub string {
Subroutine string: called CC=10 SCAR=23.2
Pod undocumented
171 189 my ( $self, $args ) = @_;
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 2 ▶sub start {
Subroutine start: called CC=1 SCAR=0.0
Pod documented
230 2 my ($self) = @_;
231  
232 2 return $self->tt(
233 { filename => 'start_block',
234 render_data => $self->render_data_non_recursive,
235 } );
236 }
237  
238 2 ▶sub end {
Subroutine end: called CC=1 SCAR=0.0
Pod documented
239 2 my ($self) = @_;
240  
241 2 return $self->tt(
242 { filename => 'end_block',
243 render_data => $self->render_data_non_recursive,
244 } );
245 }
246  
247 26 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
248 26 my $self = shift;
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 170 __PACKAGE__->meta->make_immutable;
262  
263 170 1;
264  
265 __END__
« lib/HTML/FormFu/Role/Element/Field.pm ↑ Summary lib/HTML/FormFu/Role/Element/ProcessOptionsFromModel.pm »