lib/HTML/FormFu/Role/Element/Layout.pm

filter: Statementstmt 96.4% 137 / 142 Branchbran 75.0% 42 / 56 Conditioncond 53.4% 23 / 43 MC/DCmcdc 28.0% 7 / 25 Subroutinesub 100% 26 / 26 Podpod 0.0% 0 / 3 totaltotal 79.6% 235 / 295 SCAR 39.8
CC
45
Cov
84%
layout
11.8
multi_layout
11.8
_parse_layout
10.3
CRAP 53.6
« lib/HTML/FormFu/Element/Date.pm ↑ Summary lib/HTML/FormFu/Role/Element/FieldMethods.pm »
1 383 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Role::Element::Layout;
4 383 $HTML::FormFu::Role::Element::Layout::VERSION = '2.06';
5 383 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 383 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 383 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 383 ▶use List::MoreUtils qw( first_index );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 383 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 383 ▶use HTML::FormFu::Util qw( process_attrs );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 383 has layout_errors_filename =>
15 ( is => 'rw', traits => ['Chained'], default => 'field_layout_errors' );
16 383 has layout_label_filename =>
17 ( is => 'rw', traits => ['Chained'], default => 'field_layout_label' );
18 383 has layout_field_filename =>
19 ( is => 'rw', traits => ['Chained'], default => 'field_layout_field' );
20 383 has layout_comment_filename =>
21 ( is => 'rw', traits => ['Chained'], default => 'field_layout_comment' );
22 383 has layout_javascript_filename =>
23 ( is => 'rw', traits => ['Chained'], default => 'field_layout_javascript' );
24 383 has layout_label_text_filename =>
25 ( is => 'rw', traits => ['Chained'], default => 'field_layout_label_text' );
26 383 has layout_block_filename =>
27 ( is => 'rw', traits => ['Chained'], default => 'field_layout_block' );
28  
29 383 has layout_parser_filename =>
30 ( is => 'rw', traits => ['Chained'], default => 'field_layout_parser' );
31  
32 has _layout => (
33 is => 'rw',
34 default => sub {
35 1168 ▶ return [ 'errors', 'label', 'field', 'comment', 'javascript', ];
Subroutine __ANON__: called CC=1 SCAR=0.0
36 },
37 383 );
38  
39 # if we ever remove the reverse_single() method, we can make layout()
40 # a standard Moose attribute
41  
42 sub layout {
43 2185 ▶ my $self = shift;
Subroutine layout: called CC=10 SCAR=24.7
Pod undocumented
44  
45 2185 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 128 2057 2185
46 128 $self->_layout(@_);
47 128 return $self;
48 }
49  
50 2057 my $value = $self->_layout;
51  
52 2057 ▶ if ( defined $value && $self->reverse_single ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $self->reverse_single) 4 2053 2057
Condition66%2/3
defined $value and $self->reverse_single
!ll&&!rl&&r
020534
MC/DC50%1/2
defined $value and $self->reverse_single
defined $value $self->reverse_single
Truth table66%2/3
A and B
A= defined $valueB= $self->reverse_single
ABresult
0X0
100
111
53  
54 # if it's an array-ref,
55 # and 'label' and 'field' are consecutive values (in any order)
56 # then just swap them around
57 # otherwise warn that reverse_single() is deprecated
58  
59 4 my ( $ok, $field_index, $label_index );
60  
61 4 ▶ if ( ref $value && 'ARRAY' eq reftype($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and "ARRAY" eq builtin::reftype($value)) 4 0 4
Condition33%1/3
ref $value and "ARRAY" eq builtin::reftype($value)
!ll&&!rl&&r
004
MC/DC0%0/2
ref $value and "ARRAY" eq builtin::reftype($value)
ref $value "ARRAY" eq builtin::reftype($value)
Truth table33%1/3
A and B
A= ref $valueB= "ARRAY" eq builtin::reftype($value)
ABresult
0X0
100
111
62 4 ▶ $field_index = first_index { 'field' eq $_ } @$value;
Subroutine __ANON__: called CC=1 SCAR=0.0
63 4 ▶ $label_index = first_index { 'label' eq $_ } @$value;
Subroutine __ANON__: called CC=1 SCAR=0.0
64  
65 4 ▶ if ( defined $field_index
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field_index and defined $label_index and 1 == abs $field_index - $label_index) 4 0 4
Condition33%2/6
defined $field_index and defined $label_index and 1 == abs $field_index - $label_index
!ll&&!rl&&r
004
defined $field_index and defined $label_index
!ll&&!rl&&r
004
MC/DC0%0/3
defined $field_index and defined $label_index and 1 == abs $field_index - $label_index
defined $field_index defined $label_index 1 == abs $field_index - $label_index
Truth table25%1/4
A and B and C
A= defined $field_indexB= defined $label_indexC= 1 == abs $field_index - $label_index
ABCresult
0XX0
10X0
1100
1111
66 && defined $label_index
67 && 1 == abs( $field_index - $label_index ) )
68 {
69 4 $ok = 1;
70 }
71 }
72  
73 4 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 4 0 4
74  
75 # create new arrayref so we don't change the stored value
76 4 $value = [@$value];
77  
78 4 @$value[$field_index] = 'label';
79 4 @$value[$label_index] = 'field';
80 }
81 else {
82 0 carp "reverse_single() is deprecated, and is having no affect.";
83 }
84 }
85  
86 2057 return $value;
87 }
88  
89 has _multi_layout => (
90 is => 'rw',
91 default => sub {
92 1168 ▶ return [ 'label', 'field', ];
Subroutine __ANON__: called CC=1 SCAR=0.0
93 },
94 383 );
95  
96 # if we ever remove the reverse_multi() method, we can make multi_layout()
97 # a standard Moose attribute
98  
99 sub multi_layout {
100 1634 ▶ my $self = shift;
Subroutine multi_layout: called CC=10 SCAR=24.7
Pod undocumented
101  
102 1634 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 96 1538 1634
103 96 $self->_multi_layout(@_);
104 96 return $self;
105 }
106  
107 1538 my $value = $self->_multi_layout;
108  
109 1538 ▶ if ( defined $value && $self->reverse_multi ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $self->reverse_multi) 7 1531 1538
Condition66%2/3
defined $value and $self->reverse_multi
!ll&&!rl&&r
015317
MC/DC50%1/2
defined $value and $self->reverse_multi
defined $value $self->reverse_multi
Truth table66%2/3
A and B
A= defined $valueB= $self->reverse_multi
ABresult
0X0
100
111
110  
111 # if it's an array-ref,
112 # and 'label' and 'field' are consecutive values (in any order)
113 # then just swap them around
114 # otherwise warn that reverse_multi() is deprecated
115  
116 7 my ( $ok, $field_index, $label_index );
117  
118 7 ▶ if ( ref $value && 'ARRAY' eq reftype($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and "ARRAY" eq builtin::reftype($value)) 7 0 7
Condition33%1/3
ref $value and "ARRAY" eq builtin::reftype($value)
!ll&&!rl&&r
007
MC/DC0%0/2
ref $value and "ARRAY" eq builtin::reftype($value)
ref $value "ARRAY" eq builtin::reftype($value)
Truth table33%1/3
A and B
A= ref $valueB= "ARRAY" eq builtin::reftype($value)
ABresult
0X0
100
111
119 7 ▶ $field_index = first_index { 'field' eq $_ } @$value;
Subroutine __ANON__: called CC=1 SCAR=0.0
120 7 ▶ $label_index = first_index { 'label' eq $_ } @$value;
Subroutine __ANON__: called CC=1 SCAR=0.0
121  
122 7 ▶ if ( defined $field_index
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field_index and defined $label_index and 1 == abs $field_index - $label_index) 7 0 7
Condition33%2/6
defined $field_index and defined $label_index and 1 == abs $field_index - $label_index
!ll&&!rl&&r
007
defined $field_index and defined $label_index
!ll&&!rl&&r
007
MC/DC0%0/3
defined $field_index and defined $label_index and 1 == abs $field_index - $label_index
defined $field_index defined $label_index 1 == abs $field_index - $label_index
Truth table25%1/4
A and B and C
A= defined $field_indexB= defined $label_indexC= 1 == abs $field_index - $label_index
ABCresult
0XX0
10X0
1100
1111
123 && defined $label_index
124 && 1 == abs( $field_index - $label_index ) )
125 {
126 7 $ok = 1;
127 }
128 }
129  
130 7 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 7 0 7
131  
132 # create new arrayref so we don't change the stored value
133 7 $value = [@$value];
134  
135 7 @$value[$field_index] = 'label';
136 7 @$value[$label_index] = 'field';
137 }
138 else {
139 0 carp "reverse_multi() is deprecated, and is having no affect.";
140 }
141 }
142  
143 1538 return $value;
144 }
145  
146 after BUILD => sub {
147 1317 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
148  
149 1317 $self->filename('field_layout');
150  
151 1317 return;
152 383 };
153  
154 around render_data_non_recursive => sub {
155 1321 ▶ my ( $orig, $self, $args ) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
156  
157 1321 ▶ my $render = $self->$orig(
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 1321 0 1321
158 { layout => $self->layout,
159 multi_layout => $self->multi_layout,
160 layout_errors_filename => $self->layout_errors_filename,
161 layout_label_filename => $self->layout_label_filename,
162 layout_field_filename => $self->layout_field_filename,
163 layout_comment_filename => $self->layout_comment_filename,
164 layout_javascript_filename => $self->layout_javascript_filename,
165 layout_label_text_filename => $self->layout_label_text_filename,
166 layout_block_filename => $self->layout_block_filename,
167 layout_parser_filename => $self->layout_parser_filename,
168 $args ? %$args : (),
169 } );
170  
171 1321 return $render;
172 383 };
173  
174 sub string {
175 953 ▶ my ( $self, $args ) = @_;
Subroutine string: called CC=6 SCAR=17.9
Pod undocumented
176  
177 953 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
217736
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
178  
179 my $render
180 = exists $args->{render_data}
181 ? $args->{render_data}
182 953 ▶ : $self->render_data;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'render_data'} ? : 217 736 953
183  
184 my $layout
185 = exists $args->{layout}
186 ? $args->{layout}
187 953 ▶ : $self->layout;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'layout'} ? : 217 736 953
188  
189 953 my $html = "";
190  
191 953 ▶ if ( defined $render->{container_tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $render->{"container_tag"}) 705 248 953
192 $html .= sprintf "<%s%s>\n",
193 $render->{container_tag},
194 705 process_attrs( $render->{container_attributes} );
195 }
196  
197 953 $html .= $self->_parse_layout( $render, $layout );
198  
199 953 ▶ if ( defined $render->{container_tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $render->{"container_tag"}) 705 248 953
200 705 $html .= sprintf "\n</%s>", $render->{container_tag},;
201 }
202  
203 953 return $html;
204 }
205  
206 sub _parse_layout {
207 4945 ▶ my ( $self, $render, $layout ) = @_;
Subroutine _parse_layout: called CC=10 SCAR=23.3
208  
209 4945 ▶ croak "undefined 'layout'" if !defined $layout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $layout 0 4945 4945
210  
211 4945 my $html = "";
212  
213 4945 ▶ if ( ref $layout && 'ARRAY' eq ref $layout ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $layout and 'ARRAY' eq ref $layout) { } 955 3990 4945
elsif (ref $layout and 'HASH' eq ref $layout) { } 5 3985 3990
elsif (my $method = $self->can("_parse_layout_$layout")) { } 3985 0 3985
Condition100%3/3
ref $layout and "ARRAY" eq ref $layout
!ll&&!rl&&r
39855955
MC/DC100%2/2
ref $layout and "ARRAY" eq ref $layout
ref $layout "ARRAY" eq ref $layout
Truth table100%3/3
A and B
A= ref $layoutB= "ARRAY" eq ref $layout
ABresult
0X0
100
111
214 955 my @item_html;
215 955 for my $item (@$layout) {
216 3987 push @item_html, $self->_parse_layout( $render, $item );
217 }
218 $html .=
219 join "\n",
220 955 ▶ grep { defined && length } @item_html;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 3987 0 3987
221 }
222 ▶ elsif ( ref $layout && 'HASH' eq ref $layout ) {
Condition66%2/3
ref $layout and "HASH" eq ref $layout
!ll&&!rl&&r
398505
MC/DC50%1/2
ref $layout and "HASH" eq ref $layout
ref $layout "HASH" eq ref $layout
Truth table66%2/3
A and B
A= ref $layoutB= "HASH" eq ref $layout
ABresult
0X0
100
111
223 5 my ( $key, $value ) = %$layout;
224  
225 5 ▶ if ( my $method = $self->can("_parse_layout_$key") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->can("_parse_layout_$key")) { } 2 3 5
226 2 $html .= $self->$method( $render, $key, $value );
227 }
228 else {
229 3 $html .= $self->_parse_layout_block( $render, $key, $value );
230 }
231 }
232 elsif ( my $method = $self->can("_parse_layout_$layout") ) {
233 3985 $html .= $self->$method($render);
234 }
235 else {
236 0 croak "Unknown layout() option: '$layout'";
237 }
238  
239 4945 return $html;
240 }
241  
242 sub _parse_layout_errors {
243 703 ▶ my ( $self, $render ) = @_;
Subroutine _parse_layout_errors: called CC=1 SCAR=0.0
244  
245 703 return $self->_string_errors($render);
246 }
247  
248 sub _parse_layout_label {
249 923 ▶ my $self = shift;
Subroutine _parse_layout_label: called CC=5 SCAR=16.2
250 923 my $render = shift;
251  
252 return ""
253 unless exists $render->{label}
254 && defined $render->{label}
255 923 ▶ && length $render->{label};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $render->{"label"} and defined $render->{"label"} and length $render->{"label"} 834 89 923
Condition66%4/6
exists $render->{"label"} and defined $render->{"label"} and length $render->{"label"}
!ll&&!rl&&r
834089
exists $render->{"label"} and defined $render->{"label"}
!ll&&!rl&&r
083489
MC/DC33%1/3
exists $render->{"label"} and defined $render->{"label"} and length $render->{"label"}
exists $render->{"label"} defined $render->{"label"} length $render->{"label"}
Truth table50%2/4
A and B and C
A= exists $render->{"label"}B= defined $render->{"label"}C= length $render->{"label"}
ABCresult
0XX0
10X0
1100
1111
256  
257 89 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 87 89
258 2 my ( $tag, @content ) = @_;
259  
260 return $self->_parse_layout_block(
261 $render, $tag,
262 { attributes => $render->{label_attributes},
263 2 content => \@content,
264 },
265 );
266 }
267 else {
268 87 return $self->_string_label($render);
269 }
270 }
271  
272 sub _parse_layout_field {
273 880 ▶ my ( $self, $render ) = @_;
Subroutine _parse_layout_field: called CC=1 SCAR=0.0
274  
275 880 return $self->_string_field($render);
276 }
277  
278 sub _parse_layout_comment {
279 703 ▶ my ( $self, $render ) = @_;
Subroutine _parse_layout_comment: called CC=2 SCAR=6.9
280  
281 703 ▶ return "" if !defined $render->{comment};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $render->{"comment"} 673 30 703
282  
283 my $html = sprintf "<%s%s>\n%s\n</%s>",
284 'span',
285 process_attrs( $render->{comment_attributes} ),
286 $render->{comment},
287 30 'span';
288  
289 30 return $html;
290 }
291  
292 sub _parse_layout_javascript {
293 703 ▶ my ( $self, $render ) = @_;
Subroutine _parse_layout_javascript: called CC=2 SCAR=9.2
294  
295 703 ▶ return "" if !defined $render->{javascript};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $render->{"javascript"} 703 0 703
296  
297 my $html = sprintf qq{<script type="text/javascript">\n%s\n</script>},
298 0 $render->{javascript};
299  
300 0 return $html;
301 }
302  
303 sub _parse_layout_label_text {
304 2 ▶ my ( $self, $render ) = @_;
Subroutine _parse_layout_label_text: called CC=3 SCAR=12.5
305  
306 2 ▶ return "" unless exists $render->{label} && length $render->{label};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $render->{"label"} and length $render->{"label"} 0 2 2
Condition33%1/3
exists $render->{"label"} and length $render->{"label"}
!ll&&!rl&&r
002
MC/DC0%0/2
exists $render->{"label"} and length $render->{"label"}
exists $render->{"label"} length $render->{"label"}
Truth table33%1/3
A and B
A= exists $render->{"label"}B= length $render->{"label"}
ABresult
0X0
100
111
307  
308 2 return $render->{label};
309 }
310  
311 sub _parse_layout_block {
312 5 ▶ my ( $self, $render, $tag, $opts ) = @_;
Subroutine _parse_layout_block: called CC=4 SCAR=14.1
313  
314 5 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
50
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
315  
316 5 my $html = "<$tag";
317  
318 5 ▶ if ( $opts->{attributes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"attributes"}) 5 0 5
319 5 $html .= process_attrs( $opts->{attributes} );
320 }
321  
322 5 $html .= ">\n";
323  
324 5 ▶ if ( $opts->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"content"}) 5 0 5
325 5 $html .= $self->_parse_layout( $render, $opts->{content} );
326 }
327  
328 5 $html .= "\n</$tag>";
329  
330 5 return $html;
331 }
332  
333 383 1;
334  
335 __END__
336  
337 =pod
338  
339 =encoding UTF-8
340  
341 =head1 NAME
342  
343 HTML::FormFu::Role::Element::Layout
344  
345 =head1 VERSION
346  
347 version 2.06
348  
349 =head1 AUTHOR
350  
351 Carl Franks <cpan@fireartist.com>
352  
353 =head1 COPYRIGHT AND LICENSE
354  
355 This software is copyright (c) 2018 by Carl Franks.
356  
357 This is free software; you can redistribute it and/or modify it under
358 the same terms as the Perl 5 programming language system itself.
359  
360 =cut
« lib/HTML/FormFu/Element/Date.pm ↑ Summary lib/HTML/FormFu/Role/Element/FieldMethods.pm »