TableContent/Table.pm

filter: Statementstmt 75.5% 192 / 254 Branchbran 66.6% 36 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 89.8% 44 / 49 Podpod 93.9% 31 / 33 totaltotal 77.6% 303 / 390 CC 53 SCAR 24.6
CC
53
Cov
74%
sort
90.0
_render_vertical_table
20.0
_generate_element_attr
12.0
CRAP 11.8
↑ Summary TableContent/Element.pm »
1 package HTML::TableContent::Table;
2  
3 20 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 20 ▶use HTML::TableContent::Table::Caption;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use HTML::TableContent::Table::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use HTML::TableContent::Table::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 20 our $VERSION = '1.00';
10  
11 20 extends 'HTML::TableContent::Element';
12  
13 20 has caption => ( is => 'rw', lazy => 1 );
14  
15 has [qw(headers rows)] => (
16 is => 'rw',
17 lazy => 1,
18 702 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 20 );
20  
21 20 has '+html_tag' => (
22 default => 'table',
23 );
24  
25 20 my @html5 = qw/html5 thead tbody vertical/;
26 20 for (@html5) {
27 80 has $_ => (
28 is => 'rw',
29 lazy => 1,
30 builder => 1,
31 );
32 }
33  
34 sub _build_html5 {
35 return defined $_[0]->attributes->{html5}
36 ? delete $_[0]->attributes->{html5}
37 45 ▶ : undef;
Subroutine _build_html5: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->attributes->{'html5'} ? : 0 45 45
38 }
39  
40 sub _build_thead {
41 return defined $_[0]->attributes->{thead}
42 ? delete $_[0]->attributes->{thead}
43 0 ▶ : undef;
Subroutine _build_thead: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->attributes->{'thead'} ? : 0 0 0
44 }
45  
46 sub _build_tbody {
47 return defined $_[0]->attributes->{tbody}
48 ? delete $_[0]->attributes->{tbody}
49 0 ▶ : undef;
Subroutine _build_tbody: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->attributes->{'tbody'} ? : 0 0 0
50 }
51  
52 sub _build_vertical {
53 return defined $_[0]->attributes->{vertical}
54 ? delete $_[0]->attributes->{vertical}
55 45 ▶ : undef;
Subroutine _build_vertical: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->attributes->{'vertical'} ? : 0 45 45
56 }
57  
58 around raw => sub {
59 22 ▶ my ( $orig, $self ) = ( shift, shift );
Subroutine __ANON__: called CC=2 SCAR=7.0
60  
61 22 my $table = $self->$orig(@_);
62  
63 22 ▶ if ( defined $self->caption ) { $table->{caption} = $self->caption->text }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->caption) 0 22 22
64  
65 22 my @headers = map { $_->raw } $self->all_headers;
66 22 $table->{headers} = \@headers;
67  
68 22 my @rows = map { $_->raw} $self->all_rows;
69 22 $table->{rows} = \@rows;
70  
71 22 return $table;
72 20 };
73  
74 sub aoa {
75 11 ▶ my $aoa = [ ];
Subroutine aoa: called CC=3 SCAR=11.0
Pod documented
76
77 11 my @headers = map { $_->data->[0] } $_[0]->all_headers;
78 11 ▶ if ( scalar @headers > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @headers > 0) 10 1 11
79 10 push @{ $aoa }, \@headers;
80 }
81  
82 11 for ( $_[0]->all_rows ) {
83 41 my @row = $_->array;
84 41 push @{ $aoa }, \@row;
85 }
86
87 11 return $aoa;
88 };
89  
90 sub aoh {
91 10 ▶ my $aoh = [ ];
Subroutine aoh: called CC=2 SCAR=6.9
Pod documented
92 10 for ($_[0]->all_rows) {
93 38 my $hash = $_->hash;
94 38 push @{ $aoh }, $hash;
95 }
96 10 return $aoh;
97 };
98  
99 sub add_caption {
100 18 ▶ my $caption = HTML::TableContent::Table::Caption->new($_[1]);
Subroutine add_caption: called CC=1 SCAR=0.0
Pod documented
101 18 $_[0]->caption($caption);
102 18 return $caption;
103 }
104  
105 52 ▶sub has_caption { return $_[0]->caption ? 1 : 0 };
Subroutine has_caption: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->caption ? : 46 6 52
106  
107 145 ▶sub all_rows { return @{ $_[0]->rows }; }
Subroutine all_rows: called CC=1 SCAR=0.0
Pod documented
108  
109 sub add_row {
110 2236 ▶ my $row = HTML::TableContent::Table::Row->new($_[1]);
Subroutine add_row: called CC=1 SCAR=0.0
Pod documented
111 2236 push @{ $_[0]->rows }, $row;
112 2236 return $row;
113 }
114  
115 9208 ▶sub row_count { return scalar @{ $_[0]->rows }; }
Subroutine row_count: called CC=1 SCAR=0.0
Pod documented
116  
117 8933 ▶sub get_row { return $_[0]->rows->[ $_[1] ]; }
Subroutine get_row: called CC=1 SCAR=0.0
Pod documented
118  
119 139 ▶sub get_first_row { return $_[0]->get_row(0); }
Subroutine get_first_row: called CC=1 SCAR=0.0
Pod documented
120  
121 8748 ▶sub get_last_row { return $_[0]->get_row( $_[0]->row_count - 1 ); }
Subroutine get_last_row: called CC=1 SCAR=0.0
Pod documented
122  
123 259 ▶sub clear_row { return splice @{ $_[0]->rows }, $_[1], 1; }
Subroutine clear_row: called CC=1 SCAR=0.0
Pod documented
124  
125 1 ▶sub clear_first_row { return shift @{ $_[0]->rows }; }
Subroutine clear_first_row: called CC=1 SCAR=0.0
Pod documented
126  
127 258 ▶sub clear_last_row { return $_[0]->clear_row($_[0]->row_count - 1); }
Subroutine clear_last_row: called CC=1 SCAR=0.0
Pod documented
128  
129 1503 ▶sub all_headers { return @{ $_[0]->headers }; }
Subroutine all_headers: called CC=1 SCAR=0.0
Pod documented
130  
131 sub add_header {
132 563 ▶ my $header = HTML::TableContent::Table::Header->new($_[1]);
Subroutine add_header: called CC=1 SCAR=0.0
Pod documented
133 563 push @{ $_[0]->headers }, $header;
134 563 return $header;
135 }
136  
137 273 ▶sub header_count { return scalar @{ $_[0]->headers }; }
Subroutine header_count: called CC=1 SCAR=0.0
Pod documented
138  
139 73 ▶sub get_header { return $_[0]->headers->[ $_[1] ]; }
Subroutine get_header: called CC=1 SCAR=0.0
Pod documented
140  
141 48 ▶sub get_first_header { return $_[0]->get_header(0); }
Subroutine get_first_header: called CC=1 SCAR=0.0
Pod documented
142  
143 12 ▶sub get_last_header { return $_[0]->get_header($_[0]->header_count - 1); }
Subroutine get_last_header: called CC=1 SCAR=0.0
Pod documented
144  
145 3 ▶sub clear_header { return splice @{ $_[0]->headers }, $_[1], 1; }
Subroutine clear_header: called CC=1 SCAR=0.0
Pod documented
146  
147 1 ▶sub clear_first_header { return shift @{ $_[0]->headers } }
Subroutine clear_first_header: called CC=1 SCAR=0.0
Pod documented
148  
149 1 ▶sub clear_last_header { return $_[0]->clear_header( $_[0]->header_count - 1 ); }
Subroutine clear_last_header: called CC=1 SCAR=0.0
Pod documented
150  
151 sub _render_element {
152 52 ▶ return defined $_[0]->vertical ? $_[0]->_render_vertical_table : $_[0]->_render_horizontal_table;
Subroutine _render_element: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->vertical ? : 0 52 52
153 }
154  
155 sub _render_vertical_table {
156 0 ▶ my $args = $_[0]->attributes;
Subroutine _render_vertical_table: not called CC=4 SCAR=30.0
157  
158 0 my @table_rows = ( );
159  
160 0 ▶ if ( $_[0]->has_caption ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->has_caption) 0 0 0
161 0 push @table_rows, $_[0]->caption->render;
162 }
163
164 0 for my $header ($_[0]->all_headers) {
165 0 my @row = ( );
166 0 push @row, $header->render;
167 0 for ($header->all_cells) {
168 0 push @row, $_->render;
169 }
170 0 push @table_rows, sprintf '<tr>%s</tr>', join( '', @row);
171 }
172  
173 0 my $table = join '', @table_rows;
174  
175 0 return $table;
176 }
177  
178 sub _render_horizontal_table {
179 52 ▶ my $args = $_[0]->attributes;
Subroutine _render_horizontal_table: called CC=6 SCAR=18.3
180
181 52 my @table_rows = ( );
182
183 52 ▶ if ( $_[0]->has_caption ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->has_caption) 46 6 52
184 46 push @table_rows, $_[0]->caption->render;
185 }
186  
187 52 ▶ if ( $_[0]->header_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->header_count) 47 5 52
188 47 my @headers = map { $_->render } $_[0]->all_headers;
189 47 my $headers = sprintf '%s' x @headers, @headers;
190 47 my $header_row = sprintf '<tr>%s</tr>', $headers;
191 47 ▶ if ( $_[0]->html5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->html5) 0 47 47
192 0 my $attr = $_[0]->_generate_element_attr('thead');
193 0 $header_row = sprintf '<thead %s>%s</thead>', $attr, $header_row;
194 }
195 47 push @table_rows, $header_row;
196 }
197
198 52 ▶ if ($_[0]->row_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->row_count) 51 1 52
199 51 my @rows = map { $_->render } $_[0]->all_rows;
200 51 my $row = sprintf '%s' x @rows, @rows;
201 51 ▶ if ( $_[0]->html5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->html5) 0 51 51
202 0 my $attr = $_[0]->_generate_element_attr('tbody');
203 0 $row = sprintf '<tbody %s>%s</tbody>', $attr, $row;
204 }
205 51 push @table_rows, $row;
206 }
207
208 52 my $table = sprintf '%s' x @table_rows, @table_rows;
209 52 return $table;
210 }
211  
212 sub _generate_element_attr {
213 0 ▶ my ($self, $element) = @_;
Subroutine _generate_element_attr: not called CC=3 SCAR=24.8
214 0 my $attr = '';
215 0 ▶ if ( my $attributes = $self->$element ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attributes = $self->$element) 0 0 0
216 0 for ( keys %{ $attributes } ) {
217 0 $attr .= sprintf '%s="%s" ', $_, $attributes->{$_};
218 }
219 }
220 0 return $attr;
221 }
222  
223 sub headers_spec {
224 65 ▶ my $headers = {};
Subroutine headers_spec: called CC=1 SCAR=0.0
Pod documented
225 65 map { $headers->{ $_->lc_text }++ } $_[0]->all_headers;
226 65 return $headers;
227 }
228  
229 sub header_exists {
230 63 ▶ my ( $self, @headers ) = @_;
Subroutine header_exists: called CC=3 SCAR=11.0
Pod documented
231  
232 63 my $headers_spec = $self->headers_spec;
233 63 ▶ for (@headers) { return 1 if $headers_spec->{ lc $_ } }
Branch: 1 branch
BranchTrueFalseTotal
if $$headers_spec{lc $_} 51 28 79
234 12 return 0;
235 }
236  
237 sub get_col {
238 19 ▶ my %args = ( header => $_[1] );
Subroutine get_col: called CC=1 SCAR=0.0
Pod documented
239 19 return $_[0]->get_header_column(%args);
240 }
241  
242 sub get_col_text {
243 2 ▶ my %args = ( header => $_[1] );
Subroutine get_col_text: called CC=1 SCAR=0.0
Pod documented
244 2 return $_[0]->get_header_column_text(%args);
245 }
246  
247 sub get_header_column {
248 26 ▶ my ( $self, %args ) = @_;
Subroutine get_header_column: called CC=5 SCAR=16.1
Pod documented
249  
250 26 my @cells = ();
251 26 my $column = $args{header};
252 26 foreach my $header ( $self->all_headers ) {
253 52 ▶ if ( $header->lc_text =~ m{$column}ixms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header->lc_text =~ /$column/imsx) 26 26 52
254 26 for ( $header->all_cells ) {
255 53 push @cells, $_;
256 }
257 }
258 }
259  
260 26 ▶ if ( defined $args{dedupe} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"dedupe"}) 3 23 26
261 3 @cells = $self->_dedupe_object_array_not_losing_order(@cells);
262 }
263  
264 26 return \@cells;
265 }
266  
267 sub parse_to_column {
268 5243 ▶ my ($self, $cell) = @_;
Subroutine parse_to_column: called CC=3 SCAR=11.0
Pod documented
269
270 5243 my $row = $self->get_last_row;
271  
272 5243 my $header;
273 5243 ▶ if ( my $row_header = $row->header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row_header = $row->header) { } 39 5204 5243
274 39 $header = $row_header;
275 }
276 else {
277 5204 my $cell_index = $row->cell_count;
278 5204 $header = $self->headers->[$cell_index - 1];
279 }
280  
281 5243 ▶ return unless $header;
Branch: 1 branch
BranchTrueFalseTotal
unless $header 172 5071 5243
282  
283 5071 $cell->header($header);
284 5071 push @{ $header->cells }, $cell;
285  
286 5071 return 1;
287 }
288  
289 sub get_header_column_text {
290 7 ▶ my ( $self, %args ) = @_;
Subroutine get_header_column_text: called CC=1 SCAR=0.0
Pod documented
291 7 my $cells = $self->get_header_column(%args);
292 7 my @cell_text = map { $_->text } @{$cells};
293 7 return \@cell_text;
294 }
295  
296 sub has_nested_table_column {
297 21 ▶ my $self = shift;
Subroutine has_nested_table_column: called CC=4 SCAR=13.9
Pod documented
298 21 for my $header ( $self->all_headers ) {
299 41 for ( $header->all_cells ) {
300 63 ▶ return 1 if $_->has_nested;
Branch: 1 branch
BranchTrueFalseTotal
if $_->has_nested 19 44 63
301 }
302 }
303 2 return 0;
304 }
305  
306 sub nested_column_headers {
307 19 ▶ my $self = shift;
Subroutine nested_column_headers: called CC=3 SCAR=11.0
Pod documented
308  
309 19 my $columns = {};
310 19 for my $header ( $self->all_headers ) {
311 39 my $cell = $header->get_first_cell;
312 39 ▶ if ( $cell->has_nested ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell->has_nested) 19 20 39
313 19 $columns->{ $header->lc_text }++;
314 }
315 }
316 19 return $columns;
317 }
318  
319 sub _dedupe_object_array_not_losing_order {
320 3 ▶ my ( $self, @items ) = @_;
Subroutine _dedupe_object_array_not_losing_order: called CC=3 SCAR=11.0
321  
322 # someone could probably do this in one line :)
323 3 my %args;
324 3 my @new_items = ();
325 3 foreach my $item (@items) {
326 8 ▶ if ( !defined $args{ $item->text } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{$item->text}) 5 3 8
327 5 $args{ $item->text }++;
328 5 push @new_items, $item;
329 }
330 }
331  
332 3 return @new_items;
333 }
334  
335 sub _filter_headers {
336 49 ▶ my ( $self, @headers ) = @_;
Subroutine _filter_headers: called CC=5 SCAR=16.1
337  
338 49 my $headers = [];
339 49 foreach my $header ( $self->all_headers ) {
340 130 for (@headers) {
341 266 ▶ if ( $header->lc_text =~ m/$_/ims ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header->lc_text =~ /$_/ism) 89 177 266
342 89 push @{$headers}, $header;
343 }
344 }
345 }
346  
347 49 $self->headers($headers);
348  
349 49 foreach my $row ( $self->all_rows ) {
350 130 $row->_filter_headers($headers);
351 }
352  
353 49 return 1;
354 }
355  
356 sub clear_column {
357 2 ▶ my ($self, @headers) = @_;
Subroutine clear_column: called CC=6 SCAR=17.9
Pod undocumented
358  
359 2 my @remove_cell;
360 2 for my $index (0 .. $self->header_count - 1) {
361 4 for (@headers) {
362 4 ▶ if ( $self->headers->[$index]->lc_text =~ m/$_/ims ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->headers->[$index]->lc_text =~ /$_/ism) 2 2 4
363 2 $self->clear_header($index);
364 2 push @remove_cell, $index;
365 }
366 }
367 }
368  
369 2 foreach my $row ($self->all_rows ) {
370 62 for (@remove_cell) {
371 62 $row->clear_cell($_);
372 }
373 }
374  
375 2 return 1;
376 }
377  
378 sub sort {
379 0 ▶ my ($self, $options) = @_;
Subroutine sort: not called CC=9 SCAR=45.0
Pod undocumented
380  
381 0 ▶ if ( my $order = $options->{order}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $order = $options->{"order"}) 0 0 0
382 0 my $headers = [ ];
383 0 foreach my $header (@{ $order }) {
384 0 push @{ $headers }, map { $_ } grep { $_->text =~ m/$header/ixms } $self->all_headers;
385 }
386 0 $self->headers($headers);
387
388 0 foreach my $row ( $self->all_rows ) {
389 0 my $cells = [ ];
390 0 foreach my $header (@{ $order }) {
391 0 push @{ $cells }, grep { $_->header->text =~ m/$header/ixms } $row->all_cells;
392 }
393 0 $row->cells($cells);
394 }
395 }
396  
397 0 ▶ if ( my $order = $options->{order_template}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $order = $options->{"order_template"}) 0 0 0
398 0 my $headers = [ ];
399 0 foreach my $header (@{ $order }) {
400 0 push @{ $headers }, map { $_ } grep { $_->template_attr =~ m/$header/ixms } $self->all_headers;
401 }
402 0 $self->headers($headers);
403
404 0 foreach my $row ( $self->all_rows ) {
405 0 my $cells = [ ];
406 0 foreach my $header (@{ $order }) {
407 0 push @{ $cells }, grep { $_->header->text =~ m/$header/ixms } $row->all_cells;
408 }
409 0 $row->cells($cells);
410 }
411 }
412  
413  
414 0 return $self;
415 }
416  
417  
418 20 __PACKAGE__->meta->make_immutable;
419  
420 20 1;
421  
422 __END__
↑ Summary TableContent/Element.pm »