blib/lib/Text/Matrix.pm

filter: Statementstmt 98.9% 183 / 185 Branchbran 84.0% 42 / 50 Conditioncond 66.6% 10 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 21 / 21 Podpod 100% 12 / 12 totaltotal 93.1% 272 / 292 CC 38 SCAR 14.8
CC
38
Cov
94%
matrix
7.0
_layout
7.0
_data
6.0
CRAP 4.4
↑ Summary
1 package Text::Matrix;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use List::MoreUtils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '1.00';
11  
12 sub new
13 39 ▶{
Subroutine new: called CC=5 SCAR=16.3
Pod documented
14 39 my $this = shift;
15 39 my %options = @_;
16 39 my ( $self, $class );
17  
18 39 $self = {};
19 39 ▶ $class = ref( $this ) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0390
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
20 39 bless $self, $class;
21  
22 # Defaults.
23 39 $self->spacer( ' ' );
24  
25 # Nasty nasty. But I'm lazy and it works...
26 39 foreach my $opt ( keys( %options ) )
27 {
28 # Yes, special-case new(), someone's bound to try it if they
29 # see my ugly implementation here. :P
30 71 ▶ die "Unknown option '$opt'" if $opt eq 'new' or not $self->can( $opt );
Branch: 1 branch
BranchTrueFalseTotal
if $opt eq "new" or not $self->can($opt) 1 70 71
Condition66%2/3
$opt eq "new" or not $self->can($opt)
l!l&&r!l&&!r
0170
MC/DC50%1/2
$opt eq "new" or not $self->can($opt)
$opt eq "new" not $self->can($opt)
Truth table66%2/3
A or not(B)
A= $opt eq "new"B= not $self->can($opt)
ABresult
1X1
011
000
31 70 $self->$opt( $options{ $opt } );
32 }
33  
34 38 return( $self );
35 }
36  
37 sub _self_or_instance
38 222 ▶{
Subroutine _self_or_instance: called CC=2 SCAR=6.9
39 222 my ( $self ) = @_;
40  
41 222 ▶ return( ref( $self ) ? $self : $self->new() );
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 212 10 222
42 }
43  
44 sub rows
45 25 ▶{
Subroutine rows: called CC=1 SCAR=0.0
Pod documented
46 25 my ( $self, $rows ) = @_;
47  
48 25 $self = $self->_self_or_instance();
49  
50 25 $self->{ rows } = $rows;
51 25 delete $self->{ _layout };
52  
53 25 return( $self );
54 }
55  
56 sub columns
57 27 ▶{
Subroutine columns: called CC=1 SCAR=0.0
Pod documented
58 27 my ( $self, $columns ) = @_;
59  
60 27 $self = $self->_self_or_instance();
61  
62 27 $self->{ columns } = $columns;
63 27 delete $self->{ _layout };
64  
65 27 return( $self );
66 }
67  
68 sub cols
69 2 ▶{
Subroutine cols: called CC=1 SCAR=0.0
Pod documented
70 2 my ( $self, $columns ) = @_;
71  
72 2 return( $self->columns( $columns ) );
73 }
74  
75 sub data
76 25 ▶{
Subroutine data: called CC=1 SCAR=0.0
Pod documented
77 25 my ( $self, $data ) = @_;
78  
79 25 $self = $self->_self_or_instance();
80  
81 25 $self->{ data } = $data;
82 25 delete $self->{ _layout };
83 25 delete $self->{ _data };
84 25 delete $self->{ _mapped_data };
85  
86 25 return( $self );
87 }
88  
89 sub mapper
90 8 ▶{
Subroutine mapper: called CC=2 SCAR=7.1
Pod documented
91 8 my ( $self, $mapper ) = @_;
92  
93 8 $self = $self->_self_or_instance();
94  
95 8 ▶ if( defined( $mapper ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mapper) { } 8 0 8
96 {
97 8 $self->{ mapper } = $mapper;
98 }
99 else
100 {
101 0 delete $self->{ mapper };
102 }
103 8 delete $self->{ _layout };
104 8 delete $self->{ _mapped_data };
105  
106 8 return( $self );
107 }
108  
109 sub spacer
110 43 ▶{
Subroutine spacer: called CC=2 SCAR=7.0
Pod documented
111 43 my ( $self, $spacer ) = @_;
112  
113 43 $self = $self->_self_or_instance();
114  
115 43 ▶ $self->{ spacer } = defined( $spacer ) ? $spacer : ' ';
Branch: 1 branch
BranchTrueFalseTotal
defined $spacer ? : 43 0 43
116 43 delete $self->{ _layout };
117  
118 43 return( $self );
119 }
120  
121 sub max_width
122 7 ▶{
Subroutine max_width: called CC=2 SCAR=7.1
Pod documented
123 7 my ( $self, $max_width ) = @_;
124  
125 7 $self = $self->_self_or_instance();
126  
127 7 ▶ if( defined( $max_width ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_width) { } 7 0 7
128 {
129 7 $self->{ max_width } = $max_width;
130 }
131 else
132 {
133 0 delete $self->{ max_width };
134 }
135 7 delete $self->{ _layout };
136  
137 7 return( $self );
138 }
139  
140 sub _layout
141 65 ▶{
Subroutine _layout: called CC=7 SCAR=19.5
142 65 my ( $self ) = @_;
143 65 my ( $layout, $start_column, $data );
144  
145 65 ▶ return( $self->{ _layout } ) if $self->{ _layout };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_layout"} 42 23 65
146  
147 23 $layout = {};
148 23 $data = $self->_mapped_data();
149  
150 $layout->{ row_label_width } =
151 23 List::Util::max( map { length( $_ ) } @{$self->{ rows }} );
152  
153 $layout->{ data_width } =
154 66 List::Util::max( map { List::Util::max( map { length( $_ ) } @{$_} ) }
155 23 @{$data} );
156  
157 23 $start_column = 0;
158 23 $layout->{ sections } = [];
159 23 ▶ while( $start_column < @{$self->{ columns }} )
Branch: 1 branch
BranchTrueFalseTotal
if ($start_column < @{$self->{"columns"};}) 27 22 49
160 {
161 27 my ( $end_column, $previous_width, $block, $prefix );
162  
163 27 $previous_width = $layout->{ row_label_width } + 1;
164 27 ▶ if( defined( $self->{ max_width } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'max_width'}) { } 9 18 27
165 {
166 9 $end_column = $start_column - 1;
167 9 ▶ while( ( $end_column + 1 < @{$self->{ columns }} ) and
Branch: 1 branch
BranchTrueFalseTotal
if ($end_column + 1 < @{$self->{"columns"};} and $previous_width + length($self->{"columns"}[$end_column + 1]) <= $self->{"max_width"}) 29 9 38
Condition100%3/3
$end_column + 1 < @{$self->{"columns"};} and $previous_width + length($self->{"columns"}[$end_column + 1]) <= $self->{"max_width"}
!ll&&!rl&&r
4529
MC/DC100%2/2
$end_column + 1 < @{$self->{"columns"};} and $previous_width + length($self->{"columns"}[$end_column + 1]) <= $self->{"max_width"}
$end_column + 1 < @{$self->{"columns"};} $previous_width + length($self->{"columns"}[$end_column + 1]) <= $self->{"max_width"}
Truth table100%3/3
A and B
A= $end_column + 1 < @{$self->{"columns"};}B= $previous_width + length($self->{"columns"}[$end_column + 1]) <= $self->{"max_width"}
ABresult
0X0
100
111
168 ( ( $previous_width +
169 length( $self->{ columns }->[ $end_column + 1 ] ) ) <=
170 $self->{ max_width } ) )
171 {
172 29 $end_column++;
173 $previous_width +=
174 29 $layout->{ data_width } + length( $self->{ spacer } );
175 }
176  
177 # Can't fit even a single column... :/
178 9 ▶ return( undef ) if $end_column < $start_column;
Branch: 1 branch
BranchTrueFalseTotal
if $end_column < $start_column 1 8 9
179 }
180 else
181 {
182 18 $end_column = @{$self->{ columns }} - 1;
183 }
184  
185 26 push @{$layout->{ sections }},
186 {
187 start_column => $start_column,
188 end_column => $end_column,
189 # Maybe paging will be added at some point...
190 start_row => 0,
191 26 end_row => @{$self->{ rows }} - 1,
192 };
193  
194 26 $start_column = $end_column + 1;
195 }
196  
197 22 $self->{ _layout } = $layout;
198  
199 #use Data::Dumper;
200 #print "layout: " . Data::Dumper::Dumper( $layout ) . "\n";
201  
202 22 return( $layout );
203 }
204  
205 # The data in normal form.
206 sub _data
207 39 ▶{
Subroutine _data: called CC=6 SCAR=17.9
208 39 my ( $self ) = @_;
209 39 my ( $data );
210  
211 39 ▶ return( $self->{ _data } ) if $self->{ _data };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_data"} 16 23 39
212  
213 23 ▶ return( undef ) unless $self->{ data };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"data"} 0 23 23
214  
215 23 $data = Storable::dclone( $self->{ data } );
216  
217 23 ▶ $data = [ map { $data->{ $_ } } @{$self->{ rows }} ]
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "HASH" 3 20 23
218 if ref( $data ) eq 'HASH';
219  
220 23 foreach my $row ( @{$data} )
221 {
222 66 ▶ $row = [ map { $row->{ $_ } } @{$self->{ columns }} ]
Branch: 1 branch
BranchTrueFalseTotal
if ref $row eq "HASH" 5 61 66
223 if ref( $row ) eq 'HASH';
224 }
225  
226 23 $self->{ _data } = $data;
227 # No need to hold on to the non-normalized form.
228 23 delete $self->{ data };
229  
230 #use Data::Dumper;
231 #print "data: " . Data::Dumper::Dumper( $data ) . "\n";
232  
233 23 return( $data );
234 }
235  
236 sub _mapped_data
237 45 ▶{
Subroutine _mapped_data: called CC=5 SCAR=16.1
238 45 my ( $self ) = @_;
239 45 my ( $data, $mapper );
240  
241 45 ▶ return( $self->{ _mapped_data } ) if $self->{ _mapped_data };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_mapped_data"} 6 39 45
242  
243 39 $data = $self->_data();
244 39 ▶ return( undef ) unless defined( $data );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 39 39
245 39 ▶ return( $data ) unless $mapper = $self->{ mapper };
Branch: 1 branch
BranchTrueFalseTotal
unless $mapper = $self->{"mapper"} 33 6 39
246  
247 6 $data = Storable::dclone( $data );
248  
249 6 foreach my $row ( @{$data} )
250 {
251 15 $row = [ map { scalar( $mapper->( $_ ) ) } @{$row} ];
252 }
253  
254 6 $self->{ _mapped_data } = $data;
255  
256 #use Data::Dumper;
257 #print "mapped data: " . Data::Dumper::Dumper( $data ) . "\n";
258  
259 6 return( $data );
260 }
261  
262 sub head
263 22 ▶{
Subroutine head: called CC=4 SCAR=13.9
Pod documented
264 22 my ( $self ) = @_;
265 22 my ( @ret, $layout, $column_width );
266  
267 22 $self = $self->_self_or_instance();
268  
269 22 ▶ return( undef ) unless $layout = $self->_layout();
Branch: 1 branch
BranchTrueFalseTotal
unless $layout = $self->_layout 1 21 22
270  
271 21 $column_width = $layout->{ data_width } + length( $self->{ spacer } );
272  
273 21 @ret = ();
274 21 foreach my $section ( @{$layout->{ sections }} )
275 {
276 25 my ( $block, $prefix );
277  
278 25 $block = '';
279 25 $prefix = ' ' x ( $layout->{ row_label_width } + 1 );
280 25 foreach my $column
281 ( $section->{ start_column }..$section->{ end_column } )
282 {
283 72 $block .= $prefix . $self->{ columns }->[ $column ] . "\n";
284 72 $prefix .= '|' . ( ' ' x ( $column_width - 1 ) );
285 }
286 25 $prefix =~ s/\s+$//;
287 $block .= $prefix . "\n" .
288 ( ' ' x ( $layout->{ row_label_width } + 1 ) ) .
289 ( ( 'v' . ( ' ' x ( $column_width - 1 ) ) ) x
290 25 ( $section->{ end_column } - $section->{ start_column } ) ) .
291 "v\n\n";
292 25 push @ret, $block;
293 }
294  
295 21 return( \@ret );
296 }
297  
298 sub body
299 22 ▶{
Subroutine body: called CC=5 SCAR=16.1
Pod documented
300 22 my ( $self ) = @_;
301 22 my ( @ret, $layout, $data );
302  
303 22 $self = $self->_self_or_instance();
304  
305 22 ▶ return( undef ) unless $layout = $self->_layout();
Branch: 1 branch
BranchTrueFalseTotal
unless $layout = $self->_layout 0 22 22
306 22 ▶ return( undef ) unless $data = $self->_mapped_data();
Branch: 1 branch
BranchTrueFalseTotal
unless $data = $self->_mapped_data 0 22 22
307  
308 22 @ret = ();
309 22 foreach my $section ( @{$layout->{ sections }} )
310 {
311 26 my ( $block );
312  
313 26 $block = '';
314 26 foreach my $row
315 ( $section->{ start_row }..$section->{ end_row } )
316 {
317 $block .= sprintf( '%*s ', $layout->{ row_label_width },
318 76 $self->{ rows }->[ $row ] );
319 $block .= join( $self->{ spacer },
320 map { sprintf( '%-*s', $layout->{ data_width },
321 309 $data->[ $row ]->[ $_ ] ) }
322 76 ( $section->{ start_column }..$section->{ end_column } ) );
323 76 $block .= "\n";
324 }
325 26 push @ret, $block;
326 }
327  
328 22 return( \@ret );
329 }
330  
331 sub foot
332 21 ▶{
Subroutine foot: called CC=2 SCAR=6.9
Pod documented
333 21 my ( $self ) = @_;
334 21 my ( @ret, $layout );
335  
336 21 $self = $self->_self_or_instance();
337  
338 21 $layout = $self->_layout();
339 21 ▶ return( undef ) unless $layout;
Branch: 1 branch
BranchTrueFalseTotal
unless $layout 0 21 21
340  
341 21 @ret = ( "\n" ) x scalar( @{$layout->{ sections }} );
342 21 $ret[ $#ret ] = '';
343  
344 21 return( \@ret );
345 }
346  
347 sub matrix
348 22 ▶{
Subroutine matrix: called CC=7 SCAR=19.5
Pod documented
349 22 my ( $self, $rows, $columns, $data ) = @_;
350 22 my ( $head, $body, $foot );
351  
352 22 $self = $self->_self_or_instance();
353  
354 22 ▶ $self->rows( $rows ) if defined $rows;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rows 2 20 22
355 22 ▶ $self->columns( $columns ) if defined $columns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $columns 2 20 22
356 22 ▶ $self->data( $data ) if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 2 20 22
357  
358 22 ▶ return( undef ) unless defined( $head = $self->head() ) and
Branch: 1 branch
BranchTrueFalseTotal
unless defined($head = $self->head) and defined($body = $self->body) and defined($foot = $self->foot) 1 21 22
Condition66%4/6
defined($head = $self->head) and defined($body = $self->body) and defined($foot = $self->foot)
!ll&&!rl&&r
1021
defined($head = $self->head) and defined($body = $self->body)
!ll&&!rl&&r
1021
MC/DC33%1/3
defined($head = $self->head) and defined($body = $self->body) and defined($foot = $self->foot)
defined($head = $self->head) defined($body = $self->body) defined($foot = $self->foot)
Truth table50%2/4
A and B and C
A= defined($head = $self->head)B= defined($body = $self->body)C= defined($foot = $self->foot)
ABCresult
0XX0
10X0
1100
1111
359 defined( $body = $self->body() ) and
360 defined( $foot = $self->foot() );
361  
362 21 return( join( '', List::MoreUtils::mesh( @$head, @$body, @$foot ) ) );
363 }
364  
365 5 1;
366  
367 __END__
↑ Summary