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

filter: Statementstmt 96.4% 137 / 142 Branchbran 75.9% 41 / 54 Conditioncond 54.3% 25 / 46 MC/DCmcdc 29.6% 8 / 27 Subroutinesub 100% 26 / 26 Podpod 0.0% 0 / 3 totaltotal 79.5% 237 / 298 CC 45 SCAR 18.7
CC
45
Cov
84%
layout
11.8
multi_layout
11.8
_parse_layout
10.3
CRAP 6.5
« lib/HTML/FormFu/Element/Repeatable.pm ↑ Summary lib/HTML/FormFu/Element/Date.pm »
1 416 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Role::Element::Layout;
4 # ABSTRACT: layout role
5 416 $HTML::FormFu::Role::Element::Layout::VERSION = '2.09';
6 416 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 416 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 416 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 416 ▶use List::SomeUtils qw( first_index );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 416 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 416 ▶use HTML::FormFu::Util qw( process_attrs );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 416 has layout_errors_filename =>
16 ( is => 'rw', traits => ['Chained'], default => 'field_layout_errors' );
17 416 has layout_label_filename =>
18 ( is => 'rw', traits => ['Chained'], default => 'field_layout_label' );
19 416 has layout_field_filename =>
20 ( is => 'rw', traits => ['Chained'], default => 'field_layout_field' );
21 416 has layout_comment_filename =>
22 ( is => 'rw', traits => ['Chained'], default => 'field_layout_comment' );
23 416 has layout_javascript_filename =>
24 ( is => 'rw', traits => ['Chained'], default => 'field_layout_javascript' );
25 416 has layout_label_text_filename =>
26 ( is => 'rw', traits => ['Chained'], default => 'field_layout_label_text' );
27 416 has layout_block_filename =>
28 ( is => 'rw', traits => ['Chained'], default => 'field_layout_block' );
29  
30 416 has layout_parser_filename =>
31 ( is => 'rw', traits => ['Chained'], default => 'field_layout_parser' );
32  
33 has _layout => (
34 is => 'rw',
35 1213 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 1213 return [ 'errors', 'label', 'field', 'comment', 'javascript', ];
37 },
38 416 );
39  
40 # if we ever remove the reverse_single() method, we can make layout()
41 # a standard Moose attribute
42  
43 2242 ▶sub layout {
Subroutine layout: called CC=10 SCAR=24.7
Pod undocumented
44 2242 my $self = shift;
45  
46 2242 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 138 2104 2242
47 138 $self->_layout(@_);
48 138 return $self;
49 }
50  
51 2104 my $value = $self->_layout;
52  
53 2104 ▶ if ( defined $value && $self->reverse_single ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $self->reverse_single) 4 2100 2104
Condition66%2/3
defined $value and $self->reverse_single
!ll&&!rl&&r
021004
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
54  
55 # if it's an array-ref,
56 # and 'label' and 'field' are consecutive values (in any order)
57 # then just swap them around
58 # otherwise warn that reverse_single() is deprecated
59  
60 4 my ( $ok, $field_index, $label_index );
61  
62 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
63 4 ▶ $field_index = first_index { 'field' eq $_ } @$value;
Subroutine __ANON__: called CC=1 SCAR=0.0
64 4 ▶ $label_index = first_index { 'label' eq $_ } @$value;
Subroutine __ANON__: called CC=1 SCAR=0.0
65  
66 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
67 && defined $label_index
68 && 1 == abs( $field_index - $label_index ) )
69 {
70 4 $ok = 1;
71 }
72 }
73  
74 4 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 4 0 4
75  
76 # create new arrayref so we don't change the stored value
77 4 $value = [@$value];
78  
79 4 @$value[$field_index] = 'label';
80 4 @$value[$label_index] = 'field';
81 }
82 else {
83 0 carp "reverse_single() is deprecated, and is having no affect.";
84 }
85 }
86  
87 2104 return $value;
88 }
89  
90 has _multi_layout => (
91 is => 'rw',
92 1213 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
93 1213 return [ 'label', 'field', ];
94 },
95 416 );
96  
97 # if we ever remove the reverse_multi() method, we can make multi_layout()
98 # a standard Moose attribute
99  
100 1677 ▶sub multi_layout {
Subroutine multi_layout: called CC=10 SCAR=24.7
Pod undocumented
101 1677 my $self = shift;
102  
103 1677 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 103 1574 1677
104 103 $self->_multi_layout(@_);
105 103 return $self;
106 }
107  
108 1574 my $value = $self->_multi_layout;
109  
110 1574 ▶ if ( defined $value && $self->reverse_multi ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $self->reverse_multi) 7 1567 1574
Condition66%2/3
defined $value and $self->reverse_multi
!ll&&!rl&&r
015677
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
111  
112 # if it's an array-ref,
113 # and 'label' and 'field' are consecutive values (in any order)
114 # then just swap them around
115 # otherwise warn that reverse_multi() is deprecated
116  
117 7 my ( $ok, $field_index, $label_index );
118  
119 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
120 7 ▶ $field_index = first_index { 'field' eq $_ } @$value;
Subroutine __ANON__: called CC=1 SCAR=0.0
121 7 ▶ $label_index = first_index { 'label' eq $_ } @$value;
Subroutine __ANON__: called CC=1 SCAR=0.0
122  
123 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
124 && defined $label_index
125 && 1 == abs( $field_index - $label_index ) )
126 {
127 7 $ok = 1;
128 }
129 }
130  
131 7 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 7 0 7
132  
133 # create new arrayref so we don't change the stored value
134 7 $value = [@$value];
135  
136 7 @$value[$field_index] = 'label';
137 7 @$value[$label_index] = 'field';
138 }
139 else {
140 0 carp "reverse_multi() is deprecated, and is having no affect.";
141 }
142 }
143  
144 1574 return $value;
145 }
146  
147 1365 ▶after BUILD => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
148 1365 my $self = shift;
149  
150 1365 $self->filename('field_layout');
151  
152 1365 return;
153 416 };
154  
155 1354 ▶around render_data_non_recursive => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
156 1354 my ( $orig, $self, $args ) = @_;
157  
158 1354 ▶ my $render = $self->$orig(
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 1354 0 1354
159 { layout => $self->layout,
160 multi_layout => $self->multi_layout,
161 layout_errors_filename => $self->layout_errors_filename,
162 layout_label_filename => $self->layout_label_filename,
163 layout_field_filename => $self->layout_field_filename,
164 layout_comment_filename => $self->layout_comment_filename,
165 layout_javascript_filename => $self->layout_javascript_filename,
166 layout_label_text_filename => $self->layout_label_text_filename,
167 layout_block_filename => $self->layout_block_filename,
168 layout_parser_filename => $self->layout_parser_filename,
169 $args ? %$args : (),
170 } );
171  
172 1354 return $render;
173 416 };
174  
175 970 ▶sub string {
Subroutine string: called CC=6 SCAR=17.9
Pod undocumented
176 970 my ( $self, $args ) = @_;
177  
178 970 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
220750
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
179  
180 my $render
181 = exists $args->{render_data}
182 ? $args->{render_data}
183 970 ▶ : $self->render_data;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'render_data'} ? : 220 750 970
184  
185 my $layout
186 = exists $args->{layout}
187 ? $args->{layout}
188 970 ▶ : $self->layout;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'layout'} ? : 220 750 970
189  
190 970 my $html = "";
191  
192 970 ▶ if ( defined $render->{container_tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $render->{"container_tag"}) 719 251 970
193 $html .= sprintf "<%s%s>\n",
194 $render->{container_tag},
195 719 process_attrs( $render->{container_attributes} );
196 }
197  
198 970 $html .= $self->_parse_layout( $render, $layout );
199  
200 970 ▶ if ( defined $render->{container_tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $render->{"container_tag"}) 719 251 970
201 719 $html .= sprintf "\n</%s>", $render->{container_tag},;
202 }
203  
204 970 return $html;
205 }
206  
207 5038 ▶sub _parse_layout {
Subroutine _parse_layout: called CC=10 SCAR=23.3
208 5038 my ( $self, $render, $layout ) = @_;
209  
210 5038 ▶ croak "undefined 'layout'" if !defined $layout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $layout 0 5038 5038
211  
212 5038 my $html = "";
213  
214 5038 ▶ if ( ref $layout && 'ARRAY' eq ref $layout ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $layout and 'ARRAY' eq ref $layout) { } 972 4066 5038
elsif (ref $layout and 'HASH' eq ref $layout) { } 5 4061 4066
elsif (my $method = $self->can("_parse_layout_$layout")) { } 4061 0 4061
Condition100%3/3
ref $layout and "ARRAY" eq ref $layout
!ll&&!rl&&r
40615972
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
215 972 my @item_html;
216 972 for my $item (@$layout) {
217 4063 push @item_html, $self->_parse_layout( $render, $item );
218 }
219 $html .=
220 join "\n",
221 972 ▶ grep { defined && length } @item_html;
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
029231140
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
222 }
223 ▶ elsif ( ref $layout && 'HASH' eq ref $layout ) {
Condition66%2/3
ref $layout and "HASH" eq ref $layout
!ll&&!rl&&r
406105
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
224 5 my ( $key, $value ) = %$layout;
225  
226 5 ▶ if ( my $method = $self->can("_parse_layout_$key") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->can("_parse_layout_$key")) { } 2 3 5
227 2 $html .= $self->$method( $render, $key, $value );
228 }
229 else {
230 3 $html .= $self->_parse_layout_block( $render, $key, $value );
231 }
232 }
233 elsif ( my $method = $self->can("_parse_layout_$layout") ) {
234 4061 $html .= $self->$method($render);
235 }
236 else {
237 0 croak "Unknown layout() option: '$layout'";
238 }
239  
240 5038 return $html;
241 }
242  
243 717 ▶sub _parse_layout_errors {
Subroutine _parse_layout_errors: called CC=1 SCAR=0.0
244 717 my ( $self, $render ) = @_;
245  
246 717 return $self->_string_errors($render);
247 }
248  
249 940 ▶sub _parse_layout_label {
Subroutine _parse_layout_label: called CC=5 SCAR=16.2
250 940 my $self = shift;
251 940 my $render = shift;
252  
253 return ""
254 unless exists $render->{label}
255 && defined $render->{label}
256 940 ▶ && length $render->{label};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $render->{"label"} and defined $render->{"label"} and length $render->{"label"} 849 91 940
Condition66%4/6
exists $render->{"label"} and defined $render->{"label"} and length $render->{"label"}
!ll&&!rl&&r
849091
exists $render->{"label"} and defined $render->{"label"}
!ll&&!rl&&r
084991
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
257  
258 91 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 89 91
259 2 my ( $tag, @content ) = @_;
260  
261 return $self->_parse_layout_block(
262 $render, $tag,
263 { attributes => $render->{label_attributes},
264 2 content => \@content,
265 },
266 );
267 }
268 else {
269 89 return $self->_string_label($render);
270 }
271 }
272  
273 896 ▶sub _parse_layout_field {
Subroutine _parse_layout_field: called CC=1 SCAR=0.0
274 896 my ( $self, $render ) = @_;
275  
276 896 return $self->_string_field($render);
277 }
278  
279 717 ▶sub _parse_layout_comment {
Subroutine _parse_layout_comment: called CC=2 SCAR=6.9
280 717 my ( $self, $render ) = @_;
281  
282 717 ▶ return "" if !defined $render->{comment};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $render->{"comment"} 687 30 717
283  
284 my $html = sprintf "<%s%s>\n%s\n</%s>",
285 'span',
286 process_attrs( $render->{comment_attributes} ),
287 $render->{comment},
288 30 'span';
289  
290 30 return $html;
291 }
292  
293 717 ▶sub _parse_layout_javascript {
Subroutine _parse_layout_javascript: called CC=2 SCAR=9.2
294 717 my ( $self, $render ) = @_;
295  
296 717 ▶ return "" if !defined $render->{javascript};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $render->{"javascript"} 717 0 717
297  
298 my $html = sprintf qq{<script type="text/javascript">\n%s\n</script>},
299 0 $render->{javascript};
300  
301 0 return $html;
302 }
303  
304 2 ▶sub _parse_layout_label_text {
Subroutine _parse_layout_label_text: called CC=3 SCAR=12.5
305 2 my ( $self, $render ) = @_;
306  
307 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
308  
309 2 return $render->{label};
310 }
311  
312 5 ▶sub _parse_layout_block {
Subroutine _parse_layout_block: called CC=4 SCAR=14.1
313 5 my ( $self, $render, $tag, $opts ) = @_;
314  
315 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
316  
317 5 my $html = "<$tag";
318  
319 5 ▶ if ( $opts->{attributes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"attributes"}) 5 0 5
320 5 $html .= process_attrs( $opts->{attributes} );
321 }
322  
323 5 $html .= ">\n";
324  
325 5 ▶ if ( $opts->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"content"}) 5 0 5
326 5 $html .= $self->_parse_layout( $render, $opts->{content} );
327 }
328  
329 5 $html .= "\n</$tag>";
330  
331 5 return $html;
332 }
333  
334 416 1;
335  
336 __END__
« lib/HTML/FormFu/Element/Repeatable.pm ↑ Summary lib/HTML/FormFu/Element/Date.pm »