TableContent/Element.pm

filter: Statementstmt 64.0% 116 / 181 Branchbran 60.3% 35 / 58 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 47.6% 31 / 65 Podpod 42.8% 12 / 28 totaltotal 58.8% 197 / 335 SCAR 46.0
CC
36
Cov
63%
render
39.6
__ANON__
6.1
has_data
6.0
CRAP 99.1
« TableContent/Table.pm ↑ Summary TableContent/Template/Base.pm »
1 package HTML::TableContent::Element;
2  
3 20 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use HTML::TableContent::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 our $VERSION = '0.18';
7  
8 20 my @ATTRIBUTE =
9 qw/class id style colspan rowspan onclick onchange type onkeyup placeholder scope selected value
10 autocomplete for onFocus onBlur href role width height data_toggle data_placement title/;
11  
12 around BUILDARGS => sub {
13 8631 ▶ my ( $orig, $class, $args ) = @_;
Subroutine __ANON__: called CC=6 SCAR=18.1
14  
15 8631 my $build = ();
16  
17 8631 ▶ if ( my $links = delete $args->{links} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $links = delete $args->{"links"}) 7 8624 8631
18 7 ▶ if ( ref $links eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $links eq "ARRAY") 7 0 7
19 7 $build->{links} = $links;
20 }
21 }
22  
23 8631 ▶ if ( my $concat = delete $args->{truncate} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $concat = delete $args->{"truncate"}) 0 8631 8631
24 0 $build->{truncate} = $concat;
25 }
26  
27 8631 $build->{attributes} = $args;
28 8631 $build->{attribute_list} = \@ATTRIBUTE;
29  
30 8631 for my $field ( @ATTRIBUTE, 'html_tag', 'tag') {
31 224406 ▶ if ( defined $args->{$field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{$field}) 3604 220802 224406
32 3604 $build->{$field} = $args->{$field};
33 }
34 }
35  
36 8631 return $class->$orig($build);
37 20 };
38  
39 20 for my $field (@ATTRIBUTE) {
40 480 has $field => (
41 is => 'rw',
42 lazy => 1,
43 trigger => 1,
44 default => q{}
45 );
46 }
47  
48 20 has [qw/template_attr row_index index data tag/] => (
49 is => 'rw',
50 clearer => 1,
51 builder => 1,
52 );
53  
54 has attributes => (
55 is => 'rw',
56 0 ▶ default => sub { {} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 20 );
58  
59 20 has [qw/inner_html wrap_html attribute_list truncate/] => ( is => 'rw' );
60  
61 has [qw/children nested links before_element after_element/] => (
62 is => 'rw',
63 43148 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 20 );
65  
66 20 has html_tag => (
67 is => 'rw',
68 default => 'table',
69 );
70  
71 sub _build_row_index {
72 return
73 defined $_[0]->attributes->{row_index}
74 ? delete $_[0]->attributes->{row_index}
75 8631 ▶ : undef;
Subroutine _build_row_index: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->attributes->{'row_index'} ? : 0 8631 8631
76 }
77  
78 sub _build_index {
79 return
80 defined $_[0]->attributes->{index}
81 ? delete $_[0]->attributes->{index}
82 8631 ▶ : undef;
Subroutine _build_index: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->attributes->{'index'} ? : 5 8626 8631
83 }
84  
85 sub _build_template_attr {
86 return
87 defined $_[0]->attributes->{template_attr}
88 ? delete $_[0]->attributes->{template_attr}
89 8631 ▶ : undef;
Subroutine _build_template_attr: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->attributes->{'template_attr'} ? : 262 8369 8631
90 }
91  
92 sub _build_tag {
93 8631 ▶ my $caller = caller();
Subroutine _build_tag: called CC=1 SCAR=0.0
94 8631 my ($tag) = $caller =~ /.*\:\:(.*)/;
95 8631 return lc $tag;
96 }
97  
98 0 ▶sub has_data { return scalar @{ $_[0]->data } ? 1 : 0; }
Subroutine has_data: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$_[0]->data;} ? : 0 0 0
99  
100 810 ▶sub has_children { return scalar @{ $_[0]->children } ? 1 : 0; }
Subroutine has_children: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$_[0]->children;} ? : 0 810 810
101  
102 0 ▶sub count_children { return scalar @{ $_[0]->children }; }
Subroutine count_children: not called CC=1 SCAR=6.9
Pod undocumented
103  
104 884 ▶sub has_nested { return scalar @{ $_[0]->nested } ? 1 : 0; }
Subroutine has_nested: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$_[0]->nested;} ? : 97 787 884
105  
106 30 ▶sub count_nested { return scalar @{ $_[0]->nested }; }
Subroutine count_nested: called CC=1 SCAR=0.0
Pod documented
107  
108 71 ▶sub get_first_nested { return $_[0]->nested->[0]; }
Subroutine get_first_nested: called CC=1 SCAR=0.0
Pod documented
109  
110 14 ▶sub get_nested { return $_[0]->nested->[ $_[1] ]; }
Subroutine get_nested: called CC=1 SCAR=0.0
Pod documented
111  
112 11 ▶sub all_nested { return @{ $_[0]->nested }; }
Subroutine all_nested: called CC=1 SCAR=0.0
Pod documented
113  
114 0 ▶sub has_links { return scalar @{ $_[0]->links } ? 1 : 0; }
Subroutine has_links: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$_[0]->links;} ? : 0 0 0
115  
116 0 ▶sub count_links { return scalar @{ $_[0]->links }; }
Subroutine count_links: not called CC=1 SCAR=6.9
Pod documented
117  
118 7 ▶sub get_first_link { return $_[0]->links->[0]; }
Subroutine get_first_link: called CC=1 SCAR=0.0
Pod documented
119  
120 0 ▶sub get_link { return $_[0]->links->[ $_[1] ]; }
Subroutine get_link: not called CC=1 SCAR=6.9
Pod documented
121  
122 0 ▶sub all_links { return @{ $_[0]->links }; }
Subroutine all_links: not called CC=1 SCAR=6.9
Pod documented
123  
124 sub add_child {
125 0 ▶ my $element = $_[0]->new( $_[1] );
Subroutine add_child: not called CC=1 SCAR=6.9
Pod undocumented
126 0 push @{ $_[0]->children }, $element;
127 0 return $element;
128 }
129  
130 sub add_nested {
131 1 ▶ my $table = HTML::TableContent::Table->new( $_[1] );
Subroutine add_nested: called CC=1 SCAR=0.0
Pod documented
132 1 push @{ $_[0]->nested }, $table;
133 1 return $table;
134 }
135  
136 sub add_to_nested {
137 1 ▶ return push @{ $_[0]->nested }, $_[1];
Subroutine add_to_nested: called CC=1 SCAR=0.0
Pod undocumented
138 }
139  
140 4295 ▶sub text { return join q{ }, @{ $_[0]->data }; }
Subroutine text: called CC=1 SCAR=0.0
Pod documented
141  
142 0 ▶sub truncate_text { return substr($_[0]->text, 0, $_[0]->truncate) . '...'; }
Subroutine truncate_text: not called CC=1 SCAR=6.9
Pod undocumented
143  
144 1 ▶sub add_text { return push @{ $_[0]->data }, $_[1]; }
Subroutine add_text: called CC=1 SCAR=0.0
Pod undocumented
145  
146 4 ▶sub set_text { $_[0]->data( [ $_[1] ] ); }
Subroutine set_text: called CC=1 SCAR=0.0
Pod undocumented
147  
148 763 ▶sub lc_text { return lc $_[0]->text; }
Subroutine lc_text: called CC=1 SCAR=0.0
Pod undocumented
149  
150 0 ▶sub ucf_text { return ucfirst $_[0]->text; }
Subroutine ucf_text: not called CC=1 SCAR=6.9
Pod undocumented
151  
152 sub add_class {
153 0 ▶ my $class = $_[0]->class;
Subroutine add_class: not called CC=1 SCAR=6.9
Pod undocumented
154 0 return $_[0]->class( sprintf( '%s %s', $class, $_[1] ) );
155 }
156  
157 sub add_style {
158 1 ▶ my $style = $_[0]->style;
Subroutine add_style: called CC=1 SCAR=0.0
Pod undocumented
159 1 return $_[0]->style( sprintf( '%s %s', $style, $_[1] ) );
160 }
161  
162 sub raw {
163 159 ▶ my $args = $_[0]->attributes;
Subroutine raw: called CC=4 SCAR=13.9
Pod undocumented
164  
165 159 ▶ if ( $_[0]->has_nested ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->has_nested) 10 149 159
166 10 $args->{nested} = ();
167 10 for ( $_[0]->all_nested ) {
168 8 push @{ $args->{nested} }, $_->raw;
169 }
170 }
171  
172 159 ▶ if ( scalar @{ $_[0]->data } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$_[0]->data;}) 87 72 159
173 87 $args->{text} = $_[0]->text;
174 87 $args->{data} = $_[0]->data;
175 }
176  
177 159 return $args;
178 }
179  
180 sub render {
181 810 ▶ my $args = $_[0]->attributes;
Subroutine render: called CC=16 SCAR=36.8
Pod undocumented
182  
183 810 my $attr = '';
184 810 foreach my $attribute ( @{ $_[0]->attribute_list } ) {
185 19440 ▶ if ( my $val = $args->{$attribute} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $args->{$attribute}) 453 18987 19440
186 453 $attribute =~ s/\_/\-/g;
187 453 ▶ if ( ref $val eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 453 453
elsif (ref $val eq 'CODE') { } 0 453 453
188 $val = sprintf(
189 $val->[0],
190 0 map { $_[0]->$_ } @{$val}[ 1 .. scalar @{$val} - 1 ]
191 );
192 } elsif ( ref $val eq 'CODE' ) {
193 0 $val = $val->($_[0]);
194 }
195 453 $attr .= sprintf '%s="%s" ', $attribute, $val;
196  
197 }
198 }
199  
200 810 my $render = $_[0]->_render_element;
201  
202 810 ▶ if ( my $inner_html = $_[0]->inner_html ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $inner_html = $_[0]->inner_html) 80 730 810
203 80 my $inner_count = scalar @{$inner_html};
204 80 ▶ if ( $inner_count == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner_count == 1) { } 64 16 80
205 64 $render = sprintf( $inner_html->[0], $render );
206 }
207 else {
208 $render = sprintf(
209 $inner_html->[0],
210 16 map { $_[0]->$_ } @{$inner_html}[ 1 .. $inner_count - 1 ]
211 );
212 }
213 }
214  
215 810 ▶ if ($_[0]->has_children) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->has_children) 0 810 810
216 0 my @elements = map { $_->render } @{ $_[0]->children };
217 0 my $ele_html = sprintf '%s' x @elements, @elements;
218  
219 0 $render = sprintf '%s%s', $render, $ele_html;
220 }
221  
222 810 my $tag = $_[0]->html_tag;
223 810 my $html = sprintf( "<%s %s>%s</%s>", $tag, $attr, $render, $tag );
224  
225 810 ▶ if ( my $before_element = $_[0]->before_element ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $before_element = $_[0]->before_element) 810 0 810
226 810 for ( @{$before_element} ) {
227 0 ▶ my $ren = ref \$_ eq 'SCALAR' ? $_ : $_->render;
Branch: 1 branch
BranchTrueFalseTotal
ref \$_ eq 'SCALAR' ? : 0 0 0
228 0 $html = sprintf "%s%s", $ren, $html;
229 }
230 }
231  
232 810 ▶ if ( my $after_element = $_[0]->after_element ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $after_element = $_[0]->after_element) 810 0 810
233 810 for ( @{$after_element} ) {
234 0 ▶ my $ren = ref \$_ eq 'SCALAR' ? $_ : $_->render;
Branch: 1 branch
BranchTrueFalseTotal
ref \$_ eq 'SCALAR' ? : 0 0 0
235 0 $html = sprintf "%s%s", $html, $ren;
236 }
237 }
238  
239 810 ▶ if ( my $wrap_html = $_[0]->wrap_html ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $wrap_html = $_[0]->wrap_html) 0 810 810
240 0 my $wrap_count = scalar @{$wrap_html};
241 0 ▶ if ( $wrap_count == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrap_count == 1) { } 0 0 0
242 0 $html = sprintf( $wrap_html->[0], $html );
243 }
244 else {
245 $html = sprintf(
246 $wrap_html->[0],
247 0 map { $_[0]->$_ } @{$wrap_html}[ 1 .. $wrap_html - 1 ]
248 );
249 }
250 }
251  
252 810 return $_[0]->tidy_html($html);
253 }
254  
255 sub _render_element {
256 616 ▶ return $_[0]->_render_element_text($_[0]->text);
Subroutine _render_element: called CC=1 SCAR=0.0
257 }
258  
259 sub _render_element_text {
260 616 ▶ return defined $_[0]->truncate ? $_[0]->truncate_text($_[0]->text) : $_[0]->text;
Subroutine _render_element_text: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->truncate ? : 0 616 616
261 }
262  
263 2616 ▶sub _trigger_class { return $_[0]->attributes->{class} = $_[1]; }
Subroutine _trigger_class: called CC=1 SCAR=0.0
264  
265 976 ▶sub _trigger_id { return $_[0]->attributes->{id} = $_[1]; }
Subroutine _trigger_id: called CC=1 SCAR=0.0
266  
267 18 ▶sub _trigger_style { return $_[0]->attributes->{style} = $_[1]; }
Subroutine _trigger_style: called CC=1 SCAR=0.0
268  
269 0 ▶sub _trigger_colspan { return $_[0]->attributes->{colspan} = $_[1]; }
Subroutine _trigger_colspan: not called CC=1 SCAR=6.9
270  
271 0 ▶sub _trigger_rowspan { return $_[0]->attributes->{rowspan} = $_[1]; }
Subroutine _trigger_rowspan: not called CC=1 SCAR=6.9
272  
273 0 ▶sub _trigger_onclick { return $_[0]->attributes->{onclick} = $_[1]; }
Subroutine _trigger_onclick: not called CC=1 SCAR=6.9
274  
275 0 ▶sub _trigger_onchange { return $_[0]->attributes->{onchange} = $_[1]; }
Subroutine _trigger_onchange: not called CC=1 SCAR=6.9
276  
277 0 ▶sub _trigger_type { return $_[0]->attributes->{type} = $_[1]; }
Subroutine _trigger_type: not called CC=1 SCAR=6.9
278  
279 0 ▶sub _trigger_value { return $_[0]->attributes->{value} = $_[1]; }
Subroutine _trigger_value: not called CC=1 SCAR=6.9
280  
281 0 ▶sub _trigger_scope { return $_[0]->attributes->{scope} = $_[1]; }
Subroutine _trigger_scope: not called CC=1 SCAR=6.9
282  
283 0 ▶sub _trigger_onkeyup { return $_[0]->attributes->{onkeyup} = $_[1]; }
Subroutine _trigger_onkeyup: not called CC=1 SCAR=6.9
284  
285 0 ▶sub _trigger_placeholder { return $_[0]->attributes->{placeholder} = $_[1]; }
Subroutine _trigger_placeholder: not called CC=1 SCAR=6.9
286  
287 0 ▶sub _trigger_onFocus { return $_[0]->attributes->{onFocus} = $_[1]; }
Subroutine _trigger_onFocus: not called CC=1 SCAR=6.9
288  
289 0 ▶sub _trigger_onBlur { return $_[0]->attributes->{onBlur} = $_[1]; }
Subroutine _trigger_onBlur: not called CC=1 SCAR=6.9
290  
291 0 ▶sub _trigger_role { return $_[0]->attributes->{role} = $_[1]; }
Subroutine _trigger_role: not called CC=1 SCAR=6.9
292  
293 0 ▶sub _trigger_href { return $_[0]->attributes->{href} = $_[1]; }
Subroutine _trigger_href: not called CC=1 SCAR=6.9
294  
295 0 ▶sub _trigger_width { return $_[0]->attributes->{width} = $_[1]; }
Subroutine _trigger_width: not called CC=1 SCAR=6.9
296  
297 0 ▶sub _trigger_height { return $_[0]->attributes->{height} = $_[1]; }
Subroutine _trigger_height: not called CC=1 SCAR=6.9
298  
299 0 ▶sub _trigger_data_toggle { return $_[0]->attributes->{data_toggle} = $_[1]; }
Subroutine _trigger_data_toggle: not called CC=1 SCAR=6.9
300  
301 0 ▶sub _trigger_data_placement { return $_[0]->attributes->{data_placement} = $_[1]; }
Subroutine _trigger_data_placement: not called CC=1 SCAR=6.9
302  
303 0 ▶sub _trigger_title { return $_[0]->attributes->{title} = $_[1]; }
Subroutine _trigger_title: not called CC=1 SCAR=6.9
304  
305 sub _trigger_template_attr {
306 0 ▶ return $_[0]->attributes->{template_attr} = $_[1];
Subroutine _trigger_template_attr: not called CC=1 SCAR=6.9
307 }
308  
309 0 ▶sub _trigger_selected { return $_[0]->attributes->{selected} = $_[1]; }
Subroutine _trigger_selected: not called CC=1 SCAR=6.9
310  
311 0 ▶sub _trigger_autocomplete { return $_[0]->attributes->{autocomplete} = $_[1]; }
Subroutine _trigger_autocomplete: not called CC=1 SCAR=6.9
312  
313 0 ▶sub _trigger_for { return $_[0]->attributes->{for} = $_[1]; }
Subroutine _trigger_for: not called CC=1 SCAR=6.9
314  
315 sub _build_data {
316 8631 ▶ my $data = delete $_[0]->attributes->{data};
Subroutine _build_data: called CC=4 SCAR=13.9
317 8631 my $text = delete $_[0]->attributes->{text};
318  
319 8631 ▶ if ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 3715 4916 8631
320 3715 push @{$data}, $text;
321 }
322  
323 8631 ▶ return $data if defined $data && scalar @{$data};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data and scalar @{$data;} 3715 4916 8631
Condition100%2/2
defined $data and scalar @{$data;}
!ll
49163715
MC/DC100%1/1
defined $data and scalar @{$data;}
defined $data
Truth table100%2/2
A
A= defined $data
Aresult
00
11
324 4916 return [];
325 }
326  
327 0 ▶sub has_id { return length $_[0]->id ? 1 : 0; }
Subroutine has_id: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
length $_[0]->id ? : 0 0 0
328  
329 sub tidy_html {
330 810 ▶ $_[1] =~ s/\s+>/>/g;
Subroutine tidy_html: called CC=1 SCAR=0.0
Pod undocumented
331 810 return $_[1];
332 }
333  
334 20 __PACKAGE__->meta->make_immutable;
335  
336 20 1;
337  
338 __END__
« TableContent/Table.pm ↑ Summary TableContent/Template/Base.pm »