File Coverage

blib/lib/Data/Record/Serialize/Role/Encode/CSV.pm
Criterion Covered Total %
statement 26 27 96.3
branch 1 4 25.0
condition n/a
subroutine 9 10 90.0
pod 0 2 0.0
total 36 43 83.7


line stmt bran cond sub pod time code
1             package Data::Record::Serialize::Role::Encode::CSV;
2              
3             # ABSTRACT: encode a record as csv
4              
5 5     5   271340 use Moo::Role;
  5         20210  
  5         37  
6              
7 5     5   4103 use Data::Record::Serialize::Error { errors => ['csv_backend'] }, -all;
  5         39521  
  5         89  
8 5     5   6300 use Types::Common::String qw( NonEmptyStr );
  5         627804  
  5         68  
9 5     5   7434 use Types::Standard qw( Bool );
  5         14  
  5         74  
10              
11 5     5   20950 use Text::CSV;
  5         148941  
  5         407  
12              
13 5     5   617 use namespace::clean;
  5         21033  
  5         53  
14              
15             our $VERSION = '0.03';
16              
17 6     6   3944 sub _needs_eol { 1 }
18              
19             has binary => (
20             is => 'ro',
21             isa => Bool,
22             default => 1,
23             );
24              
25             has sep_char => (
26             is => 'ro',
27             default => ','
28             );
29              
30             has quote_char => (
31             is => 'ro',
32             default => '"'
33             );
34              
35             has escape_char => (
36             is => 'ro',
37             default => '"'
38             );
39              
40             has always_quote => (
41             is => 'ro',
42             isa => Bool,
43             default => 0,
44             );
45              
46             has quote_empty => (
47             is => 'ro',
48             isa => Bool,
49             default => 0,
50             );
51              
52             has _csv => (
53             is => 'lazy',
54             init_arg => undef,
55             );
56              
57 0 0   0 0 0 sub to_bool { $_[0] ? 1 : 0 }
58              
59             sub _build__csv {
60 3     3   46 my $self = shift;
61              
62 3         84 my %args = (
63             binary => $self->binary,
64             sep_char => $self->sep_char,
65             quote_char => $self->quote_char,
66             escape_char => $self->escape_char,
67             always_quote => $self->always_quote,
68             quote_empty => $self->quote_empty,
69             auto_diag => 2,
70             );
71              
72 3         44 return Text::CSV->new( \%args );
73             }
74              
75             sub setup {
76 3     3 0 172807 my $self = shift;
77 3 50       108 $self->_csv->combine( @{ $self->output_fields } )
  3         1183  
78             or croak( 'error creating CSV header' );
79 3         391 $self->say( $self->_csv->string );
80             }
81              
82              
83              
84              
85              
86              
87              
88              
89              
90              
91             1;
92              
93             #
94             # This file is part of Data-Record-Serialize-Encode-csv
95             #
96             # This software is Copyright (c) 2022 by Smithsonian Astrophysical Observatory.
97             #
98             # This is free software, licensed under:
99             #
100             # The GNU General Public License, Version 3, June 2007
101             #
102              
103             __END__