TableContent/Template/Base.pm

filter: Statementstmt 98.9% 181 / 183 Branchbran 91.6% 66 / 72 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 24 / 24 Podpod 0.0% 0 / 1 totaltotal 95.5% 277 / 290 SCAR 39.1
CC
50
Cov
96%
_element_spec
12.0
_build_table
9.0
_num_to_en
9.0
CRAP 50.1
« TableContent/Element.pm ↑ Summary TableContent.pm »
1 package HTML::TableContent::Template::Base;
2
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 2 ▶use HTML::TableContent::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 2 our $VERSION = '0.18';
12
13 2 has table => (
14 is => 'rw',
15 builder => '_build_table',
16 clearer => 1,
17 );
18
19 2 has data => (
20 is => 'rw',
21 builder => '_build_data',
22 lazy => 1,
23 trigger => 1,
24 );
25
26 sub render {
27 46 ▶ return $_[0]->table->render;
Subroutine render: called CC=1 SCAR=0.0
Pod undocumented
28 }
29
30 sub _build_data {
31 42 ▶ return $_[0]->can('_data') ? $_[0]->_coerce_data($_[0]->_data) : [ ];
Subroutine _build_data: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->can('_data') ? : 42 0 42
32 }
33
34 sub _trigger_data {
35 4 ▶ if ( ref $_[1]->[0] eq 'ARRAY' ) {
Subroutine _trigger_data: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1][0] eq "ARRAY") 1 3 4
36 1 return $_[0]->data($_[0]->_coerce_data($_[1]));
37 }
38 3 return;
39 }
40
41 sub _coerce_data {
42 43 ▶ if ( ref $_[1]->[0] eq "ARRAY" ) {
Subroutine _coerce_data: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1][0] eq "ARRAY") 2 41 43
43 2 my $headers = shift @{ $_[1] };
44 2 my $new = [ ];
45 2 foreach my $row ( @{ $_[1] } ) {
46 6 my %hash = ( );
47 6 for (0 .. scalar @{ $row } - 1) {
48 18 $hash{$headers->[$_]} = $row->[$_];
49 }
50 6 push @{ $new }, \%hash;
51 }
52 2 return $new;
53 }
54 41 return $_[1];
55 }
56
57 sub _build_table {
58 45 ▶ my $self = shift;
Subroutine _build_table: called CC=9 SCAR=22.0
59
60 45 my $data = $self->data;
61
62 45 ▶ return unless scalar @{ $data };
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$data;} 0 45 45
63
64 45 my $table_spec = { };
65 45 ▶ if ($self->can('table_spec')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("table_spec")) 3 42 45
66 3 $table_spec = $self->table_spec;
67 }
68
69 45 my $table = HTML::TableContent::Table->new($table_spec);
70 45 $table = $self->_set_inner_html('render_table', $table);
71
72 45 my $caption_spec = $self->_caption_spec;
73
74 45 ▶ if (defined $caption_spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $caption_spec) 45 0 45
75 45 my $cap = (keys %{ $caption_spec->[0] })[0];
76 45 my $caption = $table->caption($self->$cap);
77 45 $caption = $self->_set_inner_html('render_caption', $caption);
78 }
79
80 45 my $header_spec = $self->_header_spec;
81 45 my %row_spec = $self->_row_spec;
82 45 my %cell_spec = $self->_cell_spec;
83
84 45 for (0 .. scalar @{$header_spec} - 1){
85 135 my $attr = (keys %{ $header_spec->[$_] })[0];
86 135 my $header = $self->$attr;
87
88 135 ▶ if (my $cells = delete $header->attributes->{cells}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cells = delete $header->attributes->{"cells"}) 10 125 135
89 10 $cell_spec{$_ + 1} = $cells;
90 }
91
92 135 $header = $self->_set_inner_html('render_header', $header);
93
94 135 push @{ $table->headers }, $header;
95 }
96
97 45 my $row_index = 1;
98 45 foreach my $hash ( @{ $data } ) {
99 1133 my $row_base = $self->_element_spec($row_index, %row_spec);
100
101 1133 %cell_spec = $self->_refresh_cell_spec($row_base, $row_index, %cell_spec);
102 1133 my $row = $table->add_row($row_base);
103 1133 $row = $self->_set_inner_html('render_row', $row);
104
105 1133 my $cell_index = 1;
106 1133 foreach ( $table->all_headers ) {
107 3399 my $cell_base = $self->_element_spec($cell_index++, %cell_spec);
108 3399 $cell_base->{text} = $hash->{$_->template_attr};
109 3399 my $cell = $row->add_cell($cell_base);
110 3399 $cell = $self->_set_inner_html('render_cell', $cell);
111 3399 $table->parse_to_column($cell);
112 }
113
114 1133 $row_index++;
115 }
116
117 45 ▶ if ( $self->can('last_chance') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("last_chance")) 0 45 45
118 0 $table = $self->last_chance($table);
119 }
120
121 45 return $table;
122 }
123
124 sub _element_spec {
125 4532 ▶ my ( $self, $index, %spec) = @_;
Subroutine _element_spec: called CC=12 SCAR=24.8
126
127 4532 my $base = { };
128 4532 my $row_index = delete $spec{row_index};
129
130 4532 ▶ return $base unless keys %spec;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %spec 3366 1166 4532
131
132 1166 my $num = $self->_num_to_en($index);
133
134 1166 ▶ if (defined $row_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row_index) 126 1040 1166
135 126 $num = sprintf('%s__%s', $self->_num_to_en($row_index), $num);
136 }
137
138 1166 my @pot = ($index, qw/current all odd even/, $num);
139
140 1166 for (@pot) {
141 6996 ▶ if ( my $sp = delete $spec{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sp = delete $spec{$_}) 137 6859 6996
142 137 ▶ if ( $_ =~ m{odd|even} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /odd|even/) { } 36 101 137
143 36 my $action = sprintf('_add_%s', $_);
144 36 $base = $self->$action($base, $index, $sp);
145 } else {
146 101 ▶ my $req_index = $_ =~ m{^\d$}xms ? $row_index : $index;
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^\d$/msx ? : 30 71 101
147 101 $base = $self->_add_base($base, $req_index, $sp);
148 }
149 }
150 }
151
152 1166 ▶ return $base unless keys %spec;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %spec 74 1092 1166
153
154 1092 for (keys %spec) {
155 4137 ▶ next unless defined $spec{$_}->{index};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec{$_}{"index"} 4110 27 4137
156 27 ▶ my $safe = defined $row_index ? sprintf('%s__%d', $row_index, $index) : $index;
Branch: 1 branch
BranchTrueFalseTotal
defined $row_index ? : 18 9 27
157
158 27 ▶ if ( $spec{$_}->{index} =~ m{$safe}ixms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec{$_}{"index"} =~ /$safe/imsx) 5 22 27
159 5 $base = $self->_add_to_base($base, $index, $spec{$_});
160 }
161 }
162
163 1092 return $base;
164 }
165
166 sub _add_base {
167 101 ▶ return $_[0]->_add_to_base($_[1], $_[2], $_[3]);
Subroutine _add_base: called CC=1 SCAR=0.0
168 }
169
170 sub _add_odd {
171 24 ▶ return $_[1] unless $_[2] % 2 == 1;
Subroutine _add_odd: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[2] % 2 == 1 8 16 24
172 16 return $_[0]->_add_to_base($_[1], $_[2], $_[3]);
173 }
174
175 sub _add_even {
176 12 ▶ return $_[1] unless $_[2] % 2 == 0;
Subroutine _add_even: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[2] % 2 == 0 8 4 12
177 4 return $_[0]->_add_to_base($_[1], $_[2], $_[3]);
178 }
179
180 sub _add_to_base {
181 126 ▶ my ( $self, $base, $index, $hash ) = @_;
Subroutine _add_to_base: called CC=6 SCAR=17.9
182
183 126 my @pot = (qw/increment_id cells alternate_classes/);
184 126 for (@pot) {
185 378 ▶ if ( my $p = $hash->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $hash->{$_}) 75 303 378
186 75 my $action = sprintf('_base_%s', $_);
187 75 $self->$action($p, $base, $index, $hash);
188 }
189 }
190
191 126 for ( keys %{ $hash } ) {
192 535 ▶ next if $_ =~ m{increment_id}ixms;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /increment_id/imsx 15 520 535
193
194 520 ▶ if ( $_ eq 'class' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "class") 53 467 520
195 53 $base->{$_} = $self->_join_class($hash->{$_}, $base->{$_});
196 }
197
198 520 $base->{$_} = $hash->{$_};
199 }
200
201 126 return $base;
202 }
203
204 sub _base_increment_id {
205 15 ▶ return $_[4]->{id} = sprintf('%s%s', $_[1], $_[3]);
Subroutine _base_increment_id: called CC=1 SCAR=0.0
206 }
207
208 sub _base_cells {
209 4 ▶ return $_[2]->{cells} = $_[1];
Subroutine _base_cells: called CC=1 SCAR=0.0
210 }
211
212 sub _base_alternate_classes {
213 56 ▶ my $class = shift @{ $_[1] };
Subroutine _base_alternate_classes: called CC=1 SCAR=0.0
214 56 $_[2]->{class} = $_[0]->_join_class($class, $_[2]->{class});
215 56 push @{ $_[1] }, $class;
216 }
217
218 sub _refresh_cell_spec {
219 1133 ▶ my ($self, $row_base, $row_index, %cell_spec) = @_;
Subroutine _refresh_cell_spec: called CC=5 SCAR=16.1
220
221 1133 ▶ defined $row_base->{cells} ? $cell_spec{current} = delete $row_base->{cells} : delete $cell_spec{current};
Branch: 1 branch
BranchTrueFalseTotal
defined $row_base->{'cells'} ? : 4 1129 1133
222
223 1133 $cell_spec{row_index} = $row_index;
224
225 1133 for (keys %cell_spec) {
226 1203 ▶ next unless ref $cell_spec{$_} eq 'HASH' && defined $cell_spec{$_}->{oac};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cell_spec{$_} eq "HASH" and defined $cell_spec{$_}{"oac"} 1194 9 1203
Condition100%3/3
ref $cell_spec{$_} eq "HASH" and defined $cell_spec{$_}{"oac"}
!ll&&!rl&&r
1133619
MC/DC100%2/2
ref $cell_spec{$_} eq "HASH" and defined $cell_spec{$_}{"oac"}
ref $cell_spec{$_} eq "HASH" defined $cell_spec{$_}{"oac"}
Truth table100%3/3
A and B
A= ref $cell_spec{$_} eq "HASH"B= defined $cell_spec{$_}{"oac"}
ABresult
0X0
100
111
227 9 my @classes = @{ $cell_spec{$_}->{oac} };
228 9 $cell_spec{$_}->{alternate_classes} = \@classes;
229 }
230
231 1133 return %cell_spec;
232 }
233
234 sub _join_class {
235 109 ▶ my ( $self, $class, $current ) = @_;
Subroutine _join_class: called CC=2 SCAR=6.9
236
237 109 ▶ return defined $current ? sprintf('%s %s', $current, $class) : sprintf('%s', $class);
Branch: 1 branch
BranchTrueFalseTotal
defined $current ? : 12 97 109
238 }
239
240 sub _set_inner_html {
241 4757 ▶ my ($self, $action, $element, $attr) = @_;
Subroutine _set_inner_html: called CC=6 SCAR=18.4
242
243 4757 ▶ $attr ||= $element->attributes;
Condition33%1/3
$attr ||= $element->attributes
l!l&&r!l&&!r
047570
MC/DC0%0/2
$attr ||= $element->attributes
$attr $element->attributes
Truth table33%1/3
A ||= B
A= $attrB= $element->attributes
ABresult
1X1
011
000
244
245 4757 ▶ if ( my $inner_html = delete $attr->{inner_html}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $inner_html = delete $attr->{'inner_html'}) { } 20 4737 4757
elsif ($self->can($action)) { } 58 4679 4737
246 20 ▶ if ( ref $inner_html eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $inner_html eq 'ARRAY') { } 11 9 20
elsif ($self->can($inner_html)) { } 9 0 9
247 11 $element->inner_html($inner_html);
248 } elsif ( $self->can($inner_html) ) {
249 9 $element->inner_html($self->$inner_html);
250 } else {
251 0 croak "inner_html on $element->template_attr needs to be either an ArrayRef or A reference to a Sub";
252 }
253 } elsif ( $self->can($action) ) {
254 58 $element->inner_html($self->$action);
255 }
256
257 4757 return $element;
258 }
259
260 has '_small_num_en' => (
261 is => 'ro',
262 lazy => 1,
263 default => sub {
264 26 ▶ my %NUM = ( );
Subroutine __ANON__: called CC=1 SCAR=0.0
265 26 @NUM{1 .. 20,30,40,50,60,70,80,90} = qw/
266 one two three four five six seven eight nine ten
267 eleven twelve thirteen fourteen fifteen
268 sixteen seventeen eighteen nineteen
269 twenty thirty forty fifty sixty seventy eighty ninety
270 /;
271 26 return \%NUM;
272 }
273 2 );
274
275 has '_large_num_en' => (
276 is => 'ro',
277 lazy => 1,
278 default => sub {
279 1 ▶ my %NUM = ( );
Subroutine __ANON__: called CC=1 SCAR=0.0
280 1 @NUM{3 .. 6} = qw/hundred thousand billion trillion/;
281 1 return \%NUM;
282 }
283 2 );
284
285 sub _num_to_en {
286 1292 ▶ return unless $_[1] =~ m/^\d+$/xms;
Subroutine _num_to_en: called CC=9 SCAR=22.0
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] =~ /^\d+$/msx 0 1292 1292
287
288 1292 my $small = $_[0]->_small_num_en;
289 1292 my $num = '';
290 1292 ▶ if ($num = $small->{$_[1]} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($num = $$small{$_[1]}) 318 974 1292
291 318 return $num;
292 }
293
294 974 my @numbers = split '', $_[1];
295
296 974 ▶ if ( scalar @numbers == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @numbers == 2) { } 72 902 974
297 72 return sprintf('%s_%s', $small->{$numbers[0] . 0}, $small->{$numbers[1]});
298 } else {
299 902 my $count = 0;
300 902 @numbers = reverse(@numbers);
301 902 my $string;
302 902 for (@numbers) {
303 2708 my $new = $_;
304
305 2708 ▶ if ( $new == 0 ) { $count++; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($new == 0) 185 2523 2708
306
307 2523 ▶ unless ( $count == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($count == 0) 1712 811 2523
308 1712 $new .= sprintf '%s' x $count, map { '0' } 1 .. $count;
309 }
310
311 2523 ▶ unless ($num = $small->{$new}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num = $small->{$new}) 902 1621 2523
312 902 $num = sprintf('%s_%s', $small->{$_}, $_[0]->_large_num_en->{$count + 1});
313 }
314
315 2523 ▶ $string = defined $string ? sprintf('%s_%s', $num, $string) : sprintf('%s', $num);
Branch: 1 branch
BranchTrueFalseTotal
defined $string ? : 1621 902 2523
316 2523 $count++;
317 }
318 902 return $string;
319 }
320 }
321
322 2 1;
323
324 __END__
« TableContent/Element.pm ↑ Summary TableContent.pm »