lib/Text/ASCIITable/EasyTable.pm

filter: Statementstmt 72.8% 91 / 125 Branchbran 57.5% 23 / 40 Conditioncond 56.2% 9 / 16 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 82.3% 14 / 17 Podpod 20.0% 1 / 5 totaltotal 66.2% 141 / 213 CC 30 SCAR 25.6
CC
30
Cov
68%
easy_table
30.8
_render_data
14.8
_render_table
7.1
CRAP 12.9
↑ Summary
1 package Text::ASCIITable::EasyTable;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use List::Util qw(pairs);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Text::ASCIITable;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our @EXPORT = qw(easy_table); ## no critic (ProhibitAutomaticExportation)
15  
16 1 our $VERSION = '1.006';
17  
18 ########################################################################
19 {
20 ## no critic (RequireArgUnpacking)
21  
22 9 ▶ sub is_array { push @_, 'ARRAY'; goto &_is_type; }
Subroutine is_array: called CC=1 SCAR=0.0
Pod undocumented
23 6 ▶ sub is_hash { push @_, 'HASH'; goto &_is_type; }
Subroutine is_hash: called CC=1 SCAR=0.0
Pod undocumented
24 15 ▶ sub _is_type { return ref $_[0] && reftype( $_[0] ) eq $_[1]; }
Subroutine _is_type: called CC=2 SCAR=9.2
Condition33%1/3
ref $_[0] && builtin::reftype($_[0]) eq $_[1]
!ll&&!rl&&r
0015
MC/DC0%0/2
ref $_[0] && builtin::reftype($_[0]) eq $_[1]
ref $_[0] builtin::reftype($_[0]) eq $_[1]
Truth table33%1/3
A && B
A= ref $_[0]B= builtin::reftype($_[0]) eq $_[1]
ABresult
0X0
100
111
25 }
26 ########################################################################
27  
28 ########################################################################
29 1 ▶sub uncamel {
Subroutine uncamel: not called CC=2 SCAR=14.6
Pod undocumented
30 ########################################################################
31 0 my ($str) = @_;
32  
33 0 ▶ while ( $str =~ s/^(.)(.*?)([[:upper:]])/\l$1$2_\l$3/xsmg ) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/^(.)(.*?)([[:upper:]])/\l$1$2_\l$3/gmsx) 0 0 0
34  
35 0 return $str;
36 }
37  
38 ########################################################################
39 0 ▶sub wordify {
Subroutine wordify: not called CC=1 SCAR=6.9
Pod undocumented
40 ########################################################################
41 0 my ($str) = @_;
42  
43 0 $str = uncamel($str);
44  
45 0 $str =~ s/_(.)/ \u$1/xsmg;
46  
47 0 return ucfirst $str;
48 }
49  
50 ########################################################################
51 6 ▶sub easy_table {
Subroutine easy_table: called CC=11 SCAR=34.3
Pod documented
52 ########################################################################
53 6 my (%options) = @_;
54  
55 die "'data' must be ARRAY\n"
56 6 ▶ if !is_array $options{data};
Branch: 1 branch
BranchTrueFalseTotal
unless is_array($options{"data"}) 0 6 6
57  
58 6 my @columns;
59  
60 6 ▶ if ( $options{columns} && !$options{index} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($options{'columns'} and not $options{'index'}) { } 0 6 6
elsif ($options{'rows'}) { } 3 3 6
elsif ($options{'index'}) { } 0 3 3
Condition33%1/3
$options{"columns"} and not $options{"index"}
!ll&&!rl&&r
600
MC/DC0%0/2
$options{"columns"} and not $options{"index"}
$options{"columns"} not $options{"index"}
Truth table33%1/3
A and not(B)
A= $options{"columns"}B= not $options{"index"}
ABresult
0X0
100
111
61 die "'columns' must be an ARRAY\n"
62 0 ▶ if !is_array $options{columns};
Branch: 1 branch
BranchTrueFalseTotal
unless is_array($options{"columns"}) 0 0 0
63  
64 0 @columns = @{ $options{columns} };
65 }
66 elsif ( $options{rows} ) {
67 die "'rows' must be ARRAY\n"
68 3 ▶ if !is_array $options{rows};
Branch: 1 branch
BranchTrueFalseTotal
unless is_array($options{"rows"}) 0 3 3
69  
70 die "'rows' must be key/value pairs\n"
71 3 ▶ if @{ $options{rows} } % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @{$options{"rows"};} % 2 0 3 3
72  
73 3 @columns = map { $_->[0] } pairs @{ $options{rows} };
74 }
75 elsif ( $options{index} ) {
76  
77 0 @columns = map { $_->[0] } pairs @{ $options{index} };
78  
79 0 my %index = @{ $options{index} };
80  
81 $options{rows} = [
82 map {
83 0 ▶ ( $_ => sub { return shift->{ $index{ shift() } } } )
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 0 } @columns
85 ];
86 }
87 else {
88 3 @columns = keys %{ $options{data}->[0] };
89 }
90  
91 6 $options{columns} = \@columns;
92  
93 6 my $data = _render_data( %options, columns => \@columns, );
94  
95 return _render_table( %options, data => $data )
96 6 ▶ if !$options{json};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"json"} 6 0 6
97  
98 # return an array of hashes
99 0 my @json_data;
100  
101 0 foreach my $row ( @{$data} ) {
102 0 my %hashed_row = map { $_ => shift @{$row} } @columns;
103 0 push @json_data, \%hashed_row;
104 }
105  
106 0 return JSON->new->pretty->encode( \@json_data );
107 }
108  
109 ########################################################################
110 6 ▶sub _render_table {
Subroutine _render_table: called CC=7 SCAR=19.6
111 ########################################################################
112 6 my (%options) = @_;
113  
114 # build a table...
115 6 my $table_options = $options{table_options};
116 6 ▶ $table_options //= {};
Condition100%2/2
$table_options //= {}
l!l
15
MC/DC100%1/1
$table_options //= {}
$table_options
Truth table100%2/2
A
A= $table_options
Aresult
11
00
117  
118 6 ▶ die "'table_options' must be HASH\n"
Branch: 1 branch
BranchTrueFalseTotal
unless is_hash($table_options) 0 6 6
119 if !is_hash $table_options;
120  
121 6 ▶ $table_options->{headingText} //= 'Table';
Condition100%2/2
$table_options->{"headingText"} //= "Table"
l!l
15
MC/DC100%1/1
$table_options->{"headingText"} //= "Table"
$table_options->{"headingText"}
Truth table100%2/2
A
A= $table_options->{"headingText"}
Aresult
11
00
122  
123 6 my $t = Text::ASCIITable->new($table_options);
124  
125 6 my @columns = @{ $options{columns} };
126  
127 6 ▶ if ( $options{fix_headings} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"fix_headings"}) 0 6 6
128 0 @columns = map { wordify $_ } @columns;
129 }
130  
131 6 $t->setCols(@columns);
132  
133 6 for ( @{ $options{data} } ) {
134  
135 21 ▶ if ( !@{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$_;}) 1 20 21
136 1 $t->addRowLine;
137 1 next;
138 }
139  
140 20 $t->addRow( @{$_} );
141 }
142  
143 6 return $t;
144 }
145  
146 ########################################################################
147 6 ▶sub _render_data {
Subroutine _render_data: called CC=12 SCAR=27.0
148 ########################################################################
149 6 my (%options) = @_;
150  
151 6 my ( $data, $rows, $columns, $sort_key ) = @options{qw(data rows columns sort_key)};
152  
153 6 my @sorted_data;
154  
155 6 ▶ if ($sort_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_key) { } 0 6 6
156 0 ▶ if ( reftype($sort_key) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($sort_key) eq 'CODE') { } 0 0 0
157 0 @sorted_data = $sort_key->( @{$data} );
158 }
159 else {
160 @sorted_data
161 0 = sort { lc $a->{$sort_key} cmp lc $b->{$sort_key} } @{$data};
162 }
163 }
164 else {
165 6 @sorted_data = @{$data};
166 }
167  
168 6 ▶ my %row_lu = $rows ? @{$rows} : ();
Branch: 1 branch
BranchTrueFalseTotal
$rows ? : 3 3 6
169  
170 6 my @rendered_data;
171  
172 6 my $row_count = 0;
173  
174 6 for my $row ( @{$data} ) {
175  
176 21 ▶ if ( !$row ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row) 1 20 21
177 1 push @rendered_data, [];
178 1 next;
179 }
180  
181 last
182 20 ▶ if defined $options{max_rows} && ++$row_count > $options{max_rows};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"max_rows"} and ++$row_count > $options{"max_rows"} 0 20 20
Condition33%1/3
defined $options{"max_rows"} and ++$row_count > $options{"max_rows"}
!ll&&!rl&&r
2000
MC/DC0%0/2
defined $options{"max_rows"} and ++$row_count > $options{"max_rows"}
defined $options{"max_rows"} ++$row_count > $options{"max_rows"}
Truth table33%1/3
A and B
A= defined $options{"max_rows"}B= ++$row_count > $options{"max_rows"}
ABresult
0X0
100
111
183  
184 20 ▶ if ($rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows) { } 9 11 20
185 push @rendered_data, [
186 map {
187 ref $row_lu{$_} && reftype( $row_lu{$_} ) eq 'CODE' ? $row_lu{$_}->( $row, $_ )
188 : $row_lu{$_} ? $row->{ $row_lu{$_} }
189 18 ▶ : $row->{$_}
Branch: 2 branches
BranchTrueFalseTotal
ref $row_lu{$_} && builtin::reftype($row_lu{$_}) eq 'CODE' ? : 9 9 18
$row_lu{$_} ? : 9 0 9
Condition66%2/3
ref $row_lu{$_} and builtin::reftype($row_lu{$_}) eq "CODE"
!ll&&!rl&&r
909
MC/DC50%1/2
ref $row_lu{$_} and builtin::reftype($row_lu{$_}) eq "CODE"
ref $row_lu{$_} builtin::reftype($row_lu{$_}) eq "CODE"
Truth table66%2/3
A and B
A= ref $row_lu{$_}B= builtin::reftype($row_lu{$_}) eq "CODE"
ABresult
0X0
100
111
190 9 } @{$columns},
191 ];
192 }
193 else {
194 11 push @rendered_data, [ @{$row}{ @{$columns} } ];
195 }
196 }
197  
198 6 return \@rendered_data;
199 }
200  
201 1 1;
202  
203 ## no critic (RequirePodSections)
204  
205 __END__
↑ Summary