lib/Template/Plugin/Table.pm

filter: Statementstmt 85.1% 69 / 81 Branchbran 67.3% 31 / 46 Conditioncond 53.3% 8 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 90.0% 9 / 10 Podpod 20.0% 1 / 5 totaltotal 72.2% 120 / 166 CC 30 SCAR 24.3
CC
30
Cov
76%
new
18.9
AUTOLOAD
12.0
col
8.1
CRAP 11.3
« blib/lib/Template/Plugin/Image.pm ↑ Summary lib/Template/Plugin/Datafile.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::Table
4 #
5 # DESCRIPTION
6 # Plugin to order a linear data set into a virtual 2-dimensional table
7 # from which row and column permutations can be fetched.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@wardley.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 2000-2022 Andy Wardley. All Rights Reserved.
14 #
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 #============================================================================
19  
20 package Template::Plugin::Table;
21  
22 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use base 'Template::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 our $VERSION = '3.106';
28 5 our $AUTOLOAD;
29  
30  
31 #------------------------------------------------------------------------
32 # new($context, \@data, \%args)
33 #
34 # This constructor method initialises the object to iterate through
35 # the data set passed by reference to a list as the first parameter.
36 # It calculates the shape of the permutation table based on the ROWS
37 # or COLS parameters specified in the $args hash reference. The
38 # OVERLAP parameter may be provided to specify the number of common
39 # items that should be shared between subsequent columns.
40 #------------------------------------------------------------------------
41  
42 18 ▶sub new {
Subroutine new: called CC=15 SCAR=29.4
Pod documented
43 18 my ($class, $context, $data, $params) = @_;
44 18 my ($size, $rows, $cols, $coloff, $overlap, $error);
45  
46 # if the data item is a reference to a Template::Iterator object,
47 # or subclass thereof, we call its get_all() method to extract all
48 # the data it contains
49 18 ▶ if (blessed($data) && $data->isa('Template::Iterator')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data) and $data->isa("Template::Iterator")) 0 18 18
Condition33%1/3
builtin::blessed($data) and $data->isa("Template::Iterator")
!ll&&!rl&&r
1800
MC/DC0%0/2
builtin::blessed($data) and $data->isa("Template::Iterator")
builtin::blessed($data) $data->isa("Template::Iterator")
Truth table33%1/3
A and B
A= builtin::blessed($data)B= $data->isa("Template::Iterator")
ABresult
0X0
100
111
50 0 ($data, $error) = $data->get_all();
51 0 ▶ return $class->error(
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
52 "iterator failed to provide data for table: ",
53 $error
54 ) if $error;
55 }
56  
57 18 ▶ return $class->error('invalid table data, expecting a list')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 0 18 18
58 unless ref $data eq 'ARRAY';
59  
60 18 ▶ $params ||= { };
Condition50%1/2
$params ||= {}
l!l
180
MC/DC0%0/1
$params ||= {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
61 18 ▶ return $class->error('invalid table parameters, expecting a hash')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params eq "HASH" 0 18 18
62 unless ref $params eq 'HASH';
63  
64 # ensure keys are folded to upper case
65 18 @$params{ map { uc } keys %$params } = values %$params;
66  
67 18 $size = scalar @$data;
68 18 ▶ $overlap = $params->{ OVERLAP } || 0;
Condition100%2/2
$params->{'OVERLAP'} || 0
l!l
216
MC/DC100%1/1
$params->{'OVERLAP'} || 0
$params->{'OVERLAP'}
Truth table100%2/2
A
A= $params->{'OVERLAP'}
Aresult
11
00
69  
70 # calculate number of columns based on a specified number of rows
71 18 ▶ if ($rows = $params->{ ROWS }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rows = $params->{'ROWS'}) { } 16 2 18
elsif ($cols = $params->{'COLS'}) { } 2 0 2
72 16 ▶ if ($size < $rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size < $rows) { } 1 15 16
73 1 $rows = $size; # pad?
74 1 $cols = 1;
75 1 $coloff = 0;
76 }
77 else {
78 15 $coloff = $rows - $overlap;
79 15 ▶ $cols = int ($size / $coloff)
Branch: 1 branch
BranchTrueFalseTotal
$size % $coloff > $overlap ? : 6 9 15
80 + ($size % $coloff > $overlap ? 1 : 0)
81 }
82 }
83 # calculate number of rows based on a specified number of columns
84 elsif ($cols = $params->{ COLS }) {
85 2 ▶ if ($size < $cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size < $cols) { } 1 1 2
86 1 $cols = $size;
87 1 $rows = 1;
88 1 $coloff = 1;
89 }
90 else {
91 1 ▶ $coloff = int ($size / $cols)
Branch: 1 branch
BranchTrueFalseTotal
$size % $cols > $overlap ? : 1 0 1
92 + ($size % $cols > $overlap ? 1 : 0);
93 1 $rows = $coloff + $overlap;
94 }
95 }
96 else {
97 0 $rows = $size;
98 0 $cols = 1;
99 0 $coloff = 0;
100 }
101  
102 bless {
103 _DATA => $data,
104 _SIZE => $size,
105 _NROWS => $rows,
106 _NCOLS => $cols,
107 _COLOFF => $coloff,
108 _OVERLAP => $overlap,
109 18 ▶ _PAD => $params->{ PAD } // 1,
Condition100%2/2
$params->{"PAD"} // 1
l!l
414
MC/DC100%1/1
$params->{"PAD"} // 1
$params->{"PAD"}
Truth table100%2/2
A
A= $params->{"PAD"}
Aresult
11
00
110 }, $class;
111 }
112  
113  
114 #------------------------------------------------------------------------
115 # row($n)
116 #
117 # Returns a reference to a list containing the items in the row whose
118 # number is specified by parameter. If the row number is undefined,
119 # it calls rows() to return a list of all rows.
120 #------------------------------------------------------------------------
121  
122 11 ▶sub row {
Subroutine row: called CC=7 SCAR=20.2
Pod undocumented
123 11 my ($self, $row) = @_;
124 my ($data, $cols, $offset, $size, $pad)
125 11 = @$self{ qw( _DATA _NCOLS _COLOFF _SIZE _PAD) };
126 11 my @set;
127  
128 # return all rows if row number not specified
129 11 ▶ return $self->rows()
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 11 11
130 unless defined $row;
131  
132 11 ▶ return () if $row >= $self->{ _NROWS } || $row < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $row >= $self->{"_NROWS"} or $row < 0 0 11 11
Condition33%1/3
$row >= $self->{"_NROWS"} or $row < 0
l!l&&r!l&&!r
0011
MC/DC0%0/2
$row >= $self->{"_NROWS"} or $row < 0
$row >= $self->{"_NROWS"} $row < 0
Truth table33%1/3
A or B
A= $row >= $self->{"_NROWS"}B= $row < 0
ABresult
1X1
011
000
133  
134 11 my $index = $row;
135  
136 11 ▶ for (my $c = 0; $c < $cols; $c++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $cols) 37 11 48
137 37 ▶ push(
Branch: 2 branches
BranchTrueFalseTotal
$index < $size ? : 36 1 37
$pad ? : 1 0 1
138 @set,
139 $index < $size
140 ? $data->[$index]
141 : ($pad ? undef : ())
142 );
143 37 $index += $offset;
144 }
145 11 return \@set;
146 }
147  
148  
149 #------------------------------------------------------------------------
150 # col($n)
151 #
152 # Returns a reference to a list containing the items in the column whose
153 # number is specified by parameter. If the column number is undefined,
154 # it calls cols() to return a list of all columns.
155 #------------------------------------------------------------------------
156  
157 34 ▶sub col {
Subroutine col: called CC=8 SCAR=20.9
Pod undocumented
158 34 my ($self, $col) = @_;
159 34 my ($data, $size) = @$self{ qw( _DATA _SIZE ) };
160 34 my ($start, $end);
161 34 my $blanks = 0;
162  
163 # return all cols if row number not specified
164 34 ▶ return $self->cols()
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col 2 32 34
165 unless defined $col;
166  
167 32 ▶ return () if $col >= $self->{ _NCOLS } || $col < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $col >= $self->{"_NCOLS"} or $col < 0 0 32 32
Condition33%1/3
$col >= $self->{"_NCOLS"} or $col < 0
l!l&&r!l&&!r
0032
MC/DC0%0/2
$col >= $self->{"_NCOLS"} or $col < 0
$col >= $self->{"_NCOLS"} $col < 0
Truth table33%1/3
A or B
A= $col >= $self->{"_NCOLS"}B= $col < 0
ABresult
1X1
011
000
168  
169 32 $start = $self->{ _COLOFF } * $col;
170 32 $end = $start + $self->{ _NROWS } - 1;
171 32 ▶ $end = $start if $end < $start;
Branch: 1 branch
BranchTrueFalseTotal
if $end < $start 1 31 32
172 32 ▶ if ($end >= $size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end >= $size) 6 26 32
173 6 $blanks = ($end - $size) + 1;
174 6 $end = $size - 1;
175 }
176 32 ▶ return () if $start >= $size;
Branch: 1 branch
BranchTrueFalseTotal
if $start >= $size 1 31 32
177 return [
178 @$data[$start..$end],
179 $self->{ _PAD }
180 31 ▶ ? ((undef) x $blanks)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_PAD'} ? : 11 20 31
181 : ()
182 ];
183 }
184  
185  
186 #------------------------------------------------------------------------
187 # rows()
188 #
189 # Returns all rows as a reference to a list of rows.
190 #------------------------------------------------------------------------
191  
192 1 ▶sub rows {
Subroutine rows: called CC=1 SCAR=0.0
Pod undocumented
193 1 my $self = shift;
194 1 return [ map { $self->row($_) } (0..$self->{ _NROWS }-1) ];
195 }
196  
197  
198 #------------------------------------------------------------------------
199 # cols()
200 #
201 # Returns all rows as a reference to a list of rows.
202 #------------------------------------------------------------------------
203  
204 6 ▶sub cols {
Subroutine cols: called CC=1 SCAR=0.0
Pod undocumented
205 6 my $self = shift;
206 6 return [ map { $self->col($_) } (0..$self->{ _NCOLS }-1) ];
207 }
208  
209  
210 #------------------------------------------------------------------------
211 # AUTOLOAD
212 #
213 # Provides read access to various internal data members.
214 #------------------------------------------------------------------------
215  
216 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=3 SCAR=24.8
217 0 my $self = shift;
218 0 my $item = $AUTOLOAD;
219 0 $item =~ s/.*:://;
220 0 ▶ return if $item eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $item eq "DESTROY" 0 0 0
221  
222 0 ▶ if ($item =~ /^(?:data|size|nrows|ncols|overlap|pad)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /^(?:data|size|nrows|ncols|overlap|pad)$/) { } 0 0 0
223 0 return $self->{ $item };
224 }
225 else {
226 0 return (undef, "no such table method: $item");
227 }
228 }
229  
230  
231  
232 5 1;
233  
234 __END__
« blib/lib/Template/Plugin/Image.pm ↑ Summary lib/Template/Plugin/Datafile.pm »