File Coverage

blib/lib/TablesRole/Util/CSV.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition 1 3 33.3
subroutine 3 3 100.0
pod 1 1 100.0
total 22 24 91.6


line stmt bran cond sub pod time code
1             package TablesRole::Util::CSV;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2020-11-10'; # DATE
5             our $DIST = 'TablesRoles-Standard'; # DIST
6             our $VERSION = '0.006'; # VERSION
7              
8 2     2   970 use 5.010001;
  2         8  
9 2     2   12 use Role::Tiny;
  2         5  
  2         11  
10             requires 'get_column_names';
11             requires 'get_row_arrayref';
12             requires 'reset_iterator';
13              
14             sub as_csv {
15 2     2 1 2149 require Text::CSV_XS;
16 2         30609 my $self = shift;
17              
18 2   33     32 $self->{csv_parser} //= Text::CSV_XS->new({binary=>1});
19 2         250 my $csv = $self->{csv_parser};
20              
21 2         12 $self->reset_iterator;
22              
23 2         10 my $res = "";
24 2         11 $csv->combine($self->get_column_names);
25 2         61 $res .= $csv->string . "\n";
26 2         23 while (my $row = $self->get_row_arrayref) {
27 6         20 $csv->combine(@$row);
28 6         86 $res .= $csv->string . "\n";
29             }
30 2         14 $res;
31             }
32              
33             1;
34             # ABSTRACT: Provide as_csv() and other CSV-related methods
35              
36             __END__