Grid/CSV.pm

filter: Statementstmt 39.7% 35 / 88 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 53.3% 8 / 15 Podpod 100% 5 / 5 totaltotal 28.5% 48 / 168 CC 22 SCAR 38.5
CC
22
Cov
26%
__ANON__
110.0
next
56.0
rewind
30.0
CRAP 47.0
« Grid.pm ↑ Summary Grid/Row.pm »
1 package Data::Grid::CSV;
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 extends 'Data::Grid';
10  
11 1 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Data::Grid::CSV - CSV driver for Data::Grid
16  
17 =head1 VERSION
18  
19 Version 0.06
20  
21 =cut
22  
23 1 our $VERSION = '0.07';
24  
25 =head2 new
26  
27 =cut
28  
29 1 has _csv => (
30 is => 'ro',
31 required => 1,
32 );
33  
34 0 ▶around BUILDARGS => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
35 0 my ($orig, $class, @rest) = @_;
36  
37 # normalize the params
38 0 ▶ my $p = (@rest && ref $rest[0] eq 'HASH') ? $rest[0] : { @rest };
Branch: 1 branch
BranchTrueFalseTotal
@rest && ref $rest[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@rest and ref $rest[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@rest and ref $rest[0] eq "HASH"
@rest ref $rest[0] eq "HASH"
Truth table0%0/3
A and B
A= @restB= ref $rest[0] eq "HASH"
ABresult
0X0
100
111
39 0 ▶ my $o = $p->{options} || { binary => 1 };
Condition0%0/2
$p->{'options'} || {'binary', 1}
l!l
00
MC/DC0%0/1
$p->{'options'} || {'binary', 1}
$p->{'options'}
Truth table0%0/2
A
A= $p->{'options'}
Aresult
11
00
40  
41 0 ▶ unless ($o->{sep_char} || $o->{sep}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($o->{"sep_char"} or $o->{"sep"}) 0 0 0
Condition0%0/3
$o->{"sep_char"} or $o->{"sep"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$o->{"sep_char"} or $o->{"sep"}
$o->{"sep_char"} $o->{"sep"}
Truth table0%0/3
A or B
A= $o->{"sep_char"}B= $o->{"sep"}
ABresult
1X1
011
000
42 0 my $fh = $p->{fh};
43 0 my ($commas, $tabs, $i) = (0, 0, 0);
44  
45 # we need to detect the damn thing; scan up to 10 lines
46 0 seek $fh, 0, 0;
47  
48 0 ▶ while ($i++ < 10 && (my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i++ < 10 and my $line = readline $fh) 0 0 0
Condition0%0/3
$i++ < 10 and my $line = readline $fh
!ll&&!rl&&r
000
MC/DC0%0/2
$i++ < 10 and my $line = readline $fh
$i++ < 10 my $line = readline $fh
Truth table0%0/3
A and B
A= $i++ < 10B= my $line = readline $fh
ABresult
0X0
100
111
49 # goatse operator will squash to a count
50 0 $commas += () = $line =~ /,/g;
51 0 $tabs += () = $line =~ /\t/g;
52 }
53 0 seek $fh, 0, 0;
54  
55 0 ▶ $o->{sep_char} = $commas > $tabs ? ',' : "\t";
Branch: 1 branch
BranchTrueFalseTotal
$commas > $tabs ? : 0 0 0
56 }
57  
58 # instantiate the csv driver
59 0 ▶ $p->{_csv} = Text::CSV->new($o) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
$p->{"_csv"} = "Text::CSV"->new($o) or die $! 0 0 0
60  
61 0 $class->$orig($p);
62 1 };
63  
64 =head2 tables
65  
66 =cut
67  
68 0 ▶sub tables {
Subroutine tables: not called CC=1 SCAR=6.9
Pod documented
69 0 my $self = shift;
70  
71 0 my %p = $self->table_params(0);
72  
73 # require Data::Dumper;
74 # warn Data::Dumper::Dumper(\%p);
75  
76 0 my @table = ($self->table_class->new(%p));
77 }
78  
79 =head2 table_class
80  
81 =cut
82  
83 1 has '+table_class' => (
84 default => 'Data::Grid::CSV::Table',
85 );
86  
87 =head2 row_class
88  
89 =cut
90  
91 1 has '+row_class' => (
92 default => 'Data::Grid::CSV::Row',
93 );
94  
95 =head2 cell_class
96  
97 =cut
98  
99 1 has '+cell_class' => (
100 default => 'Data::Grid::CSV::Cell',
101 );
102  
103 package Data::Grid::CSV::Table;
104  
105 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 1 extends 'Data::Grid::Table';
108  
109 # sub BUILD {
110 # my $self = shift;
111  
112 # # here is where we process
113 # }
114  
115 # has '+header' => (
116 # trigger => sub {
117 # my ($self, $val) = @_;
118 # },
119 # );
120  
121 # has '+cursor' => (
122 # trigger => sub {
123 # warn "hi lol $_[1] -> " . $_[0]->cursor;
124 # },
125 # );
126  
127 # byte offset cache
128 has _cache => (
129 is => 'ro',
130 0 ▶ default => sub { [0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 1 );
132  
133 0 ▶sub rewind {
Subroutine rewind: not called CC=5 SCAR=34.0
Pod documented
134 0 my $self = shift;
135 # we actually want to seek the file to the position after the offset
136  
137 0 my $p = $self->parent;
138 0 my $fh = $p->fh;
139 0 my $csv = $p->_csv;
140 0 my $off = $self->_offset;
141 0 my $cache = $self->_cache;
142  
143 0 ▶ if (defined $cache->[$off]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache->[$off]) { } 0 0 0
144 0 seek $fh, $cache->[$off], 0;
145 }
146 else {
147 0 seek $fh, 0, 0;
148 # start this at 1 because we already have position 0 (it's 0)
149 0 for my $i (1..$off) {
150 0 $csv->getline($fh);
151 0 ▶ last if eof $fh or $csv->eof;
Branch: 1 branch
BranchTrueFalseTotal
if eof $fh or $csv->eof 0 0 0
Condition0%0/3
eof $fh or $csv->eof
l!l&&r!l&&!r
000
MC/DC0%0/2
eof $fh or $csv->eof
eof $fh $csv->eof
Truth table0%0/3
A or B
A= eof $fhB= $csv->eof
ABresult
1X1
011
000
152 0 $cache->[$i] = tell $fh;
153 }
154 # fh position will now be on the first (desired) record
155 }
156  
157 0 $self->_set_cursor($off);
158 }
159  
160 0 ▶sub next {
Subroutine next: not called CC=7 SCAR=40.3
Pod documented
161 0 my $self = shift;
162 0 my $p = $self->parent;
163 0 my $fh = $p->fh;
164 0 my $csv = $p->_csv;
165  
166 0 my $cache = $self->_cache;
167 0 my $cursor = $self->cursor;
168  
169 0 ▶ if (defined (my $pos = $cache->[$cursor])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $pos = $cache->[$cursor])) 0 0 0
170 0 seek $fh, $pos, 0;
171 }
172  
173 # attempt to get the row
174 0 my $row = $csv->getline($fh);
175 0 ▶ return if !$row and $csv->eof;
Branch: 1 branch
BranchTrueFalseTotal
if not $row and $csv->eof 0 0 0
Condition0%0/3
not $row and $csv->eof
!ll&&!rl&&r
000
MC/DC0%0/2
not $row and $csv->eof
not $row $csv->eof
Truth table0%0/3
not(A) and B
A= not $rowB= $csv->eof
ABresult
0X0
100
111
176  
177 # i guess $csv->eof doesn't get set until after you hit the file's eof
178 0 ▶ $cache->[$cursor + 1] //= tell $fh unless eof $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless eof $fh 0 0 0
Condition0%0/3
$$cache[$cursor + 1] //= tell $fh
l!l&&r!l&&!r
000
MC/DC0%0/2
$$cache[$cursor + 1] //= tell $fh
$$cache[$cursor + 1] tell $fh
Truth table0%0/3
A //= B
A= $$cache[$cursor + 1]B= tell $fh
ABresult
1X1
011
000
179  
180 # clean up and get out of here
181 0 $self->_set_cursor($cursor + 1);
182  
183 0 ▶ $p->row_class->new($self, $cursor, $row) if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 0 0
184 }
185  
186 package Data::Grid::CSV::Row;
187  
188 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
189  
190 1 extends 'Data::Grid::Row';
191  
192 0 ▶sub cells {
Subroutine cells: not called CC=3 SCAR=24.8
Pod documented
193 0 my ($self, $flatten) = @_;
194  
195 0 my $class = $self->parent->parent->cell_class;
196 0 ▶ my @cells = @{$self->proxy || [] };
Condition0%0/2
$self->proxy || []
l!l
00
MC/DC0%0/1
$self->proxy || []
$self->proxy
Truth table0%0/2
A
A= $self->proxy
Aresult
11
00
197  
198 0 @cells = map { $class->new($self, $_, $cells[$_]) } (0..$#cells);
199  
200 0 ▶ wantarray ? @cells : \@cells;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
201 }
202  
203 package Data::Grid::CSV::Cell;
204  
205 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
206  
207 1 extends 'Data::Grid::Cell';
208  
209 0 ▶sub value {
Subroutine value: not called CC=1 SCAR=6.9
Pod documented
210 0 $_[0]->proxy;
211 }
212  
213 =head1 AUTHOR
214  
215 Dorian Taylor, C<< <dorian at cpan.org> >>
216  
217 =head1 SEE ALSO
218  
219 =over 4
220  
221 =item
222  
223 L<Data::Grid>
224  
225 =item
226  
227 L<Text::CSV_XS>
228  
229 =back
230  
231 =head1 COPYRIGHT & LICENSE
232  
233 Copyright 2010-2018 Dorian Taylor.
234  
235 Licensed under the Apache License, Version 2.0 (the "License"); you
236 may not use this file except in compliance with the License. You may
237 obtain a copy of the License at
238 L<http://www.apache.org/licenses/LICENSE-2.0>.
239  
240 Unless required by applicable law or agreed to in writing, software
241 distributed under the License is distributed on an "AS IS" BASIS,
242 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
243 implied. See the License for the specific language governing
244 permissions and limitations under the License.
245  
246 =cut
247  
248 1 1; # End of Data::Grid::CSV
« Grid.pm ↑ Summary Grid/Row.pm »