Grid/Table.pm

filter: Statementstmt 41.1% 35 / 85 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 40.9% 9 / 22 Podpod 88.8% 8 / 9 totaltotal 35.3% 52 / 147 CC 15 SCAR 25.3
CC
15
Cov
31%
BUILD
30.0
columns
30.0
rows
20.0
CRAP 12.6
« Grid/Row.pm ↑ Summary Grid/Excel.pm »
1 package Data::Grid::Table;
2  
3 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use overload '<>' => "next";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use overload '@{}' => "rows";
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Types::Standard qw(slurpy Undef Bool Int Str Maybe Optional ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Types::XSD::Lite qw(PositiveInteger NonNegativeInteger);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Type::Params qw(compile multisig Invocant);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 extends 'Data::Grid::Container';
17  
18 =encoding utf8
19  
20 =head1 NAME
21  
22 Data::Grid::Table - A table implementation for Data::Grid
23  
24 =head1 VERSION
25  
26 Version 0.06
27  
28 =cut
29  
30 1 our $VERSION = '0.06';
31  
32 =head1 SYNOPSIS
33  
34 my $grid = Data::Grid->parse('arbitrary.csv');
35  
36 # Just take the first one, since a CSV will only have one table.
37 my ($table) = $grid->tables;
38  
39 while (my $row = $table->next) {
40 # do some stuff
41 }
42  
43 # or
44  
45 while (my $row = <$table>) {
46 # ...
47 }
48  
49 # or
50  
51 my @rows = $table->rows;
52  
53 # or
54  
55 my @rows = @$table;
56  
57 =head1 METHODS
58  
59 =head2 new
60  
61 =cut
62  
63 0 ▶sub BUILD {
Subroutine BUILD: not called CC=5 SCAR=34.0
Pod undocumented
64 0 my $self = shift;
65  
66 # set initial columns
67 0 my @ic = @{$self->_init_cols};
68 0 ▶ $self->columns(@ic) if @ic;
Branch: 1 branch
BranchTrueFalseTotal
if @ic 0 0 0
69  
70 0 $self->ffwd($self->start);
71  
72 0 ▶ if ($self->header and my $hdr = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->header and my $hdr = $self->next) 0 0 0
Condition0%0/3
$self->header and my $hdr = $self->next
!ll&&!rl&&r
000
MC/DC0%0/2
$self->header and my $hdr = $self->next
$self->header my $hdr = $self->next
Truth table0%0/3
A and B
A= $self->headerB= my $hdr = $self->next
ABresult
0X0
100
111
73 # set the columns to the header in the absence of supplied columns
74  
75 0 my @hdr = map { $_->value } @$hdr;
76  
77 0 ▶ $self->columns(@hdr) unless @ic;
Branch: 1 branch
BranchTrueFalseTotal
unless @ic 0 0 0
78 }
79  
80 0 $self->ffwd($self->skip);
81  
82 0 return;
83 }
84  
85 =over 4
86  
87 =item start
88  
89 This non-negative integer offset tells the table to skip the first N
90 rows, I<before> looking for any header. Defaults to zero.
91  
92 =cut
93  
94 1 has start => (
95 is => 'rw',
96 isa => NonNegativeInteger,
97 default => 0,
98 );
99  
100 =item header
101  
102 This flag tells us that the first (logical) row is a header. Defaults
103 to false.
104  
105 =cut
106  
107 has header => (
108 is => 'rw',
109 isa => Bool,
110 0 ▶ coerce => sub { int !!$_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 1 default => 0,
112 );
113  
114 =item skip
115  
116 This offset tells us to skip N rows I<after> any header. Defaults to
117 zero.
118  
119 =cut
120  
121 1 has skip => (
122 is => 'rw',
123 isa => NonNegativeInteger,
124 default => 0,
125 );
126  
127 =item columns
128  
129 This C<ARRAY> reference of columns will be used in lieu of, or I<instead>
130 of, any header row. Columns (or the header row)
131  
132 =cut
133  
134 # we save the explicit initial columns as a separate member
135 has _init_cols => (
136 is => 'ro',
137 isa => ArrayRef[Str],
138 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 1 init_arg => 'columns',
140 );
141  
142 has _columns => (
143 is => 'ro',
144 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 1 init_arg => undef,
146 );
147  
148 0 ▶sub columns {
Subroutine columns: not called CC=5 SCAR=34.0
Pod documented
149 0 state $check = Type::Params::multisig(
150 [Invocant],
151 [Invocant, Undef],
152 [Invocant, slurpy ArrayRef[Str]]);
153 0 my ($self, @args) = $check->(@_);
154 0 my $cols = $self->_columns;
155  
156 # no args? this is an accessor
157 0 ▶ return wantarray ? @$cols : [@$cols] unless @args;
Branch: 2 branches
BranchTrueFalseTotal
unless @args 0 0 0
wantarray ? : 0 0 0
158  
159 0 my @old = @$cols;
160 0 ▶ @{$self->_init_cols} = @$cols = defined $args[0] ? @{$args[0]} : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $args[0] ? : 0 0 0
161  
162 0 ▶ wantarray ? @old : \@old;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
163 }
164  
165 =back
166  
167 =head2 cursor
168  
169 Returns the row offset as an integer starting with zero, which is the
170 beginning of the table I<irrespective> of offsets like L</start>,
171 L</header>, and L</skip>. Not to be confused with
172 L<Data::Grid::Container/position>.
173  
174 =cut
175  
176 0 ▶sub _offset {
Subroutine _offset: not called CC=1 SCAR=6.9
177 0 my $self = shift;
178 0 $self->start + $self->header + $self->skip;
179 }
180  
181 1 has cursor => (
182 is => 'rwp',
183 isa => NonNegativeInteger,
184 default => 0,
185 init_arg => undef,
186 );
187  
188  
189 =head2 next
190  
191 Retrieves the next row in the table or C<undef> when it reaches the
192 end. This method L<must be overridden> by a driver subclass. The
193 iteration operator C<<>> is also overloaded for table objects, so you
194 can use it like this:
195  
196 while (my $row = <$table>) { ...
197  
198 =cut
199  
200 0 ▶sub next {
Subroutine next: not called CC=1 SCAR=6.9
Pod documented
201 0 Carp::croak("This method is a stub; it must be overridden!");
202 }
203  
204 =head2 first
205  
206 Returns the first row in the table, and is equivalent to calling
207 L</rewind> and then L</next>.
208  
209 =cut
210  
211 0 ▶sub first {
Subroutine first: not called CC=1 SCAR=6.9
Pod documented
212 0 my $self = shift;
213 0 $self->rewind;
214 0 $self->next;
215 }
216  
217 =head2 rewind
218  
219 Sets the table's cursor back to the first row. Returns the previous
220 position, beginning at zero. This method I<must be overridden> by a
221 driver subclass.
222  
223 =cut
224  
225 0 ▶sub rewind {
Subroutine rewind: not called CC=1 SCAR=6.9
Pod documented
226 0 Carp::croak("This method is a stub; it must be overridden!");
227 }
228  
229 =head2 ffwd $ROWS
230  
231 Fast forward by C<$ROWS> and return what is there.
232  
233 =cut
234  
235 0 ▶sub ffwd {
Subroutine ffwd: not called CC=3 SCAR=24.8
Pod documented
236 0 state $check = Type::Params::compile(Invocant, NonNegativeInteger);
237  
238 0 my ($self, $rows) = $check->(@_);
239 0 ▶ return unless $rows;
Branch: 1 branch
BranchTrueFalseTotal
unless $rows 0 0 0
240  
241 # call in void context to skip the overhead of constructing the row
242 0 ▶ $self->next while $rows-- > 1;
Branch: 1 branch
BranchTrueFalseTotal
$rows-- > 1 and do { $self->next } 0 0 0
243  
244 0 $self->next; # returning the last one
245 }
246  
247 =head2 rows [$FLATTEN];
248  
249 Retrieves an array of rows all at once. This method overloads the
250 array dereferencing operator C<@{}>, so you can use it like this:
251  
252 my @rows = @$table;
253  
254 Note that this implementation is I<extremely naïve> and you will
255 almost invariably want to override it in a subclass.
256  
257 =cut
258  
259 0 ▶sub rows {
Subroutine rows: not called CC=4 SCAR=30.0
Pod documented
260 0 my ($self, $flatten) = @_;
261 0 my @rows;
262  
263 0 $self->rewind;
264 0 ▶ while (my $row = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->next) 0 0 0
265 0 ▶ push @rows, $flatten ? scalar $row->cells(1) : $row;
Branch: 1 branch
BranchTrueFalseTotal
$flatten ? : 0 0 0
266 }
267 0 $self->rewind;
268  
269 0 ▶ wantarray ? @rows : \@rows;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
270 }
271  
272 # =head2 columns
273  
274 # =cut
275  
276 # =head2 width
277  
278 # Gets the width, in columns, of the table.
279  
280 # =cut
281  
282 =head2 height
283  
284 Gets the height, in rows, of the table. Careful, for drivers that only
285 do sequential access, this means iterating over the whole set, so you
286 might as well.
287  
288 This implementation is naïve; please override it in a subclass.
289  
290 =cut
291  
292 0 ▶sub height {
Subroutine height: not called CC=1 SCAR=6.9
Pod documented
293 # overloaded lol
294 0 scalar @{$_[0]};
295 }
296  
297 =head2 as_string
298  
299 Returns the table as CSV.
300  
301 =cut
302  
303 0 ▶sub as_string {
Subroutine as_string: not called CC=2 SCAR=17.9
Pod documented
304 0 my $self = shift;
305 0 my $out = '';
306 0 $self->rewind;
307  
308 0 ▶ while (my $row = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->next) 0 0 0
309 0 $out .= $row->as_string . "\n";
310 }
311  
312 0 $out;
313 }
314  
315 =head1 AUTHOR
316  
317 Dorian Taylor, C<< <dorian at cpan.org> >>
318  
319 =head1 SEE ALSO
320  
321 =over 4
322  
323 =item
324  
325 L<Data::Grid>
326  
327 =item
328  
329 L<Data::Grid::Container>
330  
331 =item
332  
333 L<Data::Grid::Row>
334  
335 =item
336  
337 L<Data::Grid::Cell>
338  
339 =back
340  
341 =head1 COPYRIGHT & LICENSE
342  
343 Copyright 2010-2018 Dorian Taylor.
344  
345 Licensed under the Apache License, Version 2.0 (the "License"); you
346 may not use this file except in compliance with the License. You may
347 obtain a copy of the License at
348 L<http://www.apache.org/licenses/LICENSE-2.0>.
349  
350 Unless required by applicable law or agreed to in writing, software
351 distributed under the License is distributed on an "AS IS" BASIS,
352 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
353 implied. See the License for the specific language governing
354 permissions and limitations under the License.
355  
356 =cut
357  
358 1 1; # End of Data::Grid::Table
« Grid/Row.pm ↑ Summary Grid/Excel.pm »