File Coverage

blib/lib/AnyData2/Format/CSV.pm
Criterion Covered Total %
statement 32 40 80.0
branch 7 16 43.7
condition 1 12 8.3
subroutine 8 10 80.0
pod 4 4 100.0
total 52 82 63.4


line stmt bran cond sub pod time code
1             package AnyData2::Format::CSV;
2              
3 2     2   1367 use 5.008001;
  2         4  
4 2     2   6 use strict;
  2         2  
  2         35  
5 2     2   11 use warnings FATAL => 'all';
  2         2  
  2         54  
6              
7 2     2   6 use base qw(AnyData2::Format AnyData2::Role::GuessImplementation);
  2         2  
  2         595  
8              
9 2     2   9 use Carp 'croak';
  2         2  
  2         609  
10              
11             =head1 NAME
12              
13             AnyData2::Format::CSV - CSV format class for AnyData2
14              
15             =cut
16              
17             our $VERSION = '0.002';
18              
19             =head1 METHODS
20              
21             =head2 new
22              
23             my $af = AnyData2->new(
24             CSV => {},
25             "File::Linewise" => { filename => File::Spec->catfile( $test_dir, "simple.csv" ) }
26             );
27              
28             constructs a CSV accessor, passes all options down to C beside
29             C, C and C. C is used
30             to instantiate the parser and prefers L over L
31             by default. When C is set to a true value, the first
32             line of the csv isn't used to guess the names in C. Specifying
33             C always wins over any value of C.
34              
35             =cut
36              
37             sub new
38             {
39 1     1 1 2 my ( $class, $storage, %options ) = @_;
40 1         7 my $self = $class->SUPER::new($storage);
41              
42 1         1 my $csv_class = delete $options{csv_class};
43 1         1 my $csv_skip_first_row = delete $options{csv_skip_first_row};
44              
45 1 50       7 defined $csv_class or $csv_class = $class->_guess_suitable_class(qw(Text::CSV_XS Text::CSV));
46              
47 1         6 my $csv = $csv_class->new( {%options} );
48 1         73 $self->{csv} = $csv;
49              
50             # XXX
51 1 50 33     5 $self->cols unless ( defined $csv_skip_first_row and $csv_skip_first_row );
52              
53 1         9 $self;
54             }
55              
56             sub _handle_error
57             {
58 0     0   0 my ( $self, $code, $str, $pos, $rec, $fld ) = @_;
59 0 0 0     0 defined $pos and defined $rec and defined $fld and croak "record $rec at line $pos in $fld - $code - $str";
      0        
60 0 0 0     0 defined $pos and defined $rec and croak "record $rec at line $pos - $code - $str";
61 0         0 croak "$code - $str";
62             }
63              
64             =head2 cols
65              
66             Deliver the columns of the CSV ...
67              
68             =cut
69              
70             sub cols
71             {
72 2     2 1 5 my $self = shift;
73 2 100       4 defined $self->{csv_cols} and return $self->{csv_cols};
74 1         2 $self->{csv_cols} = $self->fetchrow;
75             }
76              
77             =head2 fetchrow
78              
79             Parses a line read from storage and return the result
80              
81             =cut
82              
83             sub fetchrow
84             {
85 5     5 1 38 my $self = shift;
86 5         11 my $buf = $self->{storage}->read();
87 5 100       8 defined $buf or return;
88 4         9 my $stat = $self->{csv}->parse($buf);
89 4 50       66 $stat or return $self->_handle_error( $self->{csv}->error_diag );
90 4         10 [ $self->{csv}->fields ];
91             }
92              
93             =head2 pushrow
94              
95             Encodes values and write to storage
96              
97             =cut
98              
99             sub pushrow
100             {
101 0     0 1   my ( $self, $fields ) = @_;
102 0           my $stat = $self->{csv}->combine(@$fields);
103 0 0         $stat or return $self->_handle_error( $self->{csv}->error_diag );
104 0           $self->{storage}->write( $self->{csv}->string );
105             }
106              
107             =head1 LICENSE AND COPYRIGHT
108              
109             Copyright 2015,2016 Jens Rehsack.
110              
111             This program is free software; you can redistribute it and/or modify it
112             under the terms of either: the GNU General Public License as published
113             by the Free Software Foundation; or the Artistic License.
114              
115             See http://dev.perl.org/licenses/ for more information.
116              
117             If your Modified Version has been derived from a Modified Version made
118             by someone other than you, you are nevertheless required to ensure that
119             your Modified Version complies with the requirements of this license.
120              
121             This license does not grant you the right to use any trademark, service
122             mark, tradename, or logo of the Copyright Holder.
123              
124             This license includes the non-exclusive, worldwide, free-of-charge
125             patent license to make, have made, use, offer to sell, sell, import and
126             otherwise transfer the Package with respect to any patent claims
127             licensable by the Copyright Holder that are necessarily infringed by the
128             Package. If you institute patent litigation (including a cross-claim or
129             counterclaim) against any party alleging that the Package constitutes
130             direct or contributory patent infringement, then this License
131             to you shall terminate on the date that such litigation is filed.
132              
133             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
134             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
135             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
136             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
137             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
138             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
139             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
140             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
141              
142             =cut
143              
144             1;