File Coverage

lib/DataFlow/Proc/CSV.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package DataFlow::Proc::CSV;
2              
3 1     1   14556 use strict;
  1         3  
  1         47  
4 1     1   5 use warnings;
  1         2  
  1         56  
5              
6             # ABSTRACT: A CSV converting processor
7              
8             our $VERSION = '1.121830'; # VERSION
9              
10 1     1   538 use Moose;
  0            
  0            
11             extends 'DataFlow::Proc::Converter';
12              
13             use namespace::autoclean;
14             use Text::CSV::Encoded;
15             use MooseX::Aliases;
16              
17             has 'header' => (
18             'is' => 'rw',
19             'isa' => 'ArrayRef[Maybe[Str]]',
20             'predicate' => 'has_header',
21             'alias' => 'headers',
22             'handles' => { 'has_headers' => sub { shift->has_header }, },
23             );
24              
25             has 'header_wanted' => (
26             'is' => 'rw',
27             'isa' => 'Bool',
28             'lazy' => 1,
29             'default' => sub {
30             my $self = shift;
31             return 0 if $self->direction eq 'CONVERT_FROM';
32             return 1 if $self->has_header;
33             return 0;
34             },
35             );
36              
37             has '+converter' => (
38             'lazy' => 1,
39             'default' => sub {
40             my $self = shift;
41             return $self->has_converter_opts
42             ? Text::CSV::Encoded->new( $self->converter_opts )
43             : Text::CSV::Encoded->new;
44             },
45             'handles' => {
46             'text_csv' => sub { shift->converter(@_) },
47             'text_csv_opts' => sub { shift->converter_opts(@_) },
48             'has_text_csv_opts' => sub { shift->has_converter_opts },
49             },
50             'init_arg' => 'text_csv',
51             );
52              
53             has '+converter_opts' => ( 'init_arg' => 'text_csv_opts', );
54              
55             sub _combine {
56             my ( $self, $e ) = @_;
57             my $status = $self->converter->combine( @{$e} );
58             die $self->converter->error_diag unless $status;
59             return $self->converter->string;
60             }
61              
62             sub _parse {
63             my ( $self, $line ) = @_;
64             my $ok = $self->converter->parse($line);
65             die $self->converter->error_diag unless $ok;
66             return [ $self->converter->fields ];
67             }
68              
69             sub _policy {
70             return shift->direction eq 'CONVERT_TO' ? 'ArrayRef' : 'Scalar';
71             }
72              
73             sub _build_subs {
74             my $self = shift;
75             return {
76             'CONVERT_TO' => sub {
77             my @res = ();
78             if ( $self->header_wanted ) {
79             $self->header_wanted(0);
80             push @res, $self->_combine( $self->header );
81             }
82              
83             push @res, $self->_combine($_);
84             return @res;
85             },
86             'CONVERT_FROM' => sub {
87             if ( $self->header_wanted ) {
88             $self->header_wanted(0);
89             $self->header( $self->_parse($_) );
90             return;
91             }
92             return $self->_parse($_);
93             },
94             };
95             }
96              
97             __PACKAGE__->meta->make_immutable;
98              
99             1;
100              
101              
102             __END__
103             =pod
104              
105             =encoding utf-8
106              
107             =head1 NAME
108              
109             DataFlow::Proc::CSV - A CSV converting processor
110              
111             =head1 VERSION
112              
113             version 1.121830
114              
115             =head1 SEE ALSO
116              
117             Please see those modules/websites for more information related to this module.
118              
119             =over 4
120              
121             =item *
122              
123             L<DataFlow|DataFlow>
124              
125             =back
126              
127             =head1 AUTHOR
128              
129             Alexei Znamensky <russoz@cpan.org>
130              
131             =head1 COPYRIGHT AND LICENSE
132              
133             This software is copyright (c) 2011 by Alexei Znamensky.
134              
135             This is free software; you can redistribute it and/or modify it under
136             the same terms as the Perl 5 programming language system itself.
137              
138             =head1 BUGS AND LIMITATIONS
139              
140             You can make new bug reports, and view existing ones, through the
141             web interface at L<http://rt.cpan.org>.
142              
143             =head1 DISCLAIMER OF WARRANTY
144              
145             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
146             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
147             WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
148             PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
149             EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
150             IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
151             PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
152             SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME
153             THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
154              
155             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
156             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
157             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
158             TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
159             CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
160             SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
161             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
162             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
163             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
164             DAMAGES.
165              
166             =cut
167