Catmandu/Exporter/CSV.pm

filter: Statementstmt 97.8% 45 / 46 Branchbran 100% 8 / 8 Conditioncond 90.9% 10 / 11 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 2 totaltotal 94.1% 81 / 86 CC 9 SCAR 10.7
CC
9
Cov
97%
_print_header
6.0
_build_csv
3.0
add
2.0
CRAP 2.9
« Catmandu/Cmd/count.pm ↑ Summary Catmandu/Exporter/Count.pm »
1 package Catmandu::Exporter::CSV;
2  
3 7 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 our $VERSION = '1.2025';
6  
7 7 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 with 'Catmandu::TabularExporter';
12  
13 7 has csv => (is => 'lazy');
14 16 ▶has quote_char => (is => 'ro', default => sub {'"'});
Subroutine __ANON__: called CC=1 SCAR=0.0
15 16 ▶has escape_char => (is => 'ro', default => sub {'"'});
Subroutine __ANON__: called CC=1 SCAR=0.0
16 7 has always_quote => (is => 'ro');
17 7 has quote_space => (is => 'ro');
18 has sep_char => (
19 is => 'ro',
20 14 ▶ default => sub {','},
Subroutine __ANON__: called CC=1 SCAR=0.0
21 21 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.2
22 21 my $sep_char = $_[0];
23 0 $sep_char =~ s/(\\[abefnrt])/"qq{$1}"/gee;
24 21 return $sep_char;
25 }
26 7 );
27  
28 17 ▶sub _build_csv {
Subroutine _build_csv: called CC=3 SCAR=11.0
29 17 my ($self) = @_;
30 17 ▶ Text::CSV->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->quote_char ? : 12 5 17
$self->escape_char ? : 12 5 17
31 {
32 binary => 1,
33 eol => "\n",
34 sep_char => $self->sep_char,
35 always_quote => $self->always_quote,
36 quote_space => $self->quote_space,
37 quote_char => $self->quote_char ? $self->quote_char : undef,
38 escape_char => $self->escape_char ? $self->escape_char : undef,
39 }
40 );
41 }
42  
43 34 ▶sub add {
Subroutine add: called CC=2 SCAR=6.9
Pod undocumented
44 34 my ($self, $data) = @_;
45 34 my $fields = $self->fields;
46 my $row = [
47 map {
48 34 ▶ my $val = $data->{$_} // "";
Condition100%2/2
$data->{$_} // ""
l!l
524
MC/DC100%1/1
$data->{$_} // ""
$data->{$_}
Truth table100%2/2
A
A= $data->{$_}
Aresult
11
00
49 56 $val =~ s/\t/\\t/g;
50 56 $val =~ s/\n/\\n/g;
51 56 $val =~ s/\r/\\r/g;
52 56 $val;
53 } @$fields
54 ];
55  
56 34 $self->_print_header;
57 34 $self->csv->print($self->fh, $row);
58 }
59  
60 12 ▶sub commit {
Subroutine commit: called CC=1 SCAR=0.0
Pod undocumented
61 12 my ($self) = @_;
62  
63 # ensure header gets printed even if there are no records
64 12 $self->_print_header;
65 }
66  
67 46 ▶sub _print_header {
Subroutine _print_header: called CC=6 SCAR=17.9
68 46 my ($self) = @_;
69 46 ▶ if (!$self->count && $self->header) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->count and $self->header) 16 30 46
Condition100%3/3
not $self->count and $self->header
!ll&&!rl&&r
27316
MC/DC100%2/2
not $self->count and $self->header
not $self->count $self->header
Truth table100%3/3
not(A) and B
A= not $self->countB= $self->header
ABresult
0X0
100
111
70 16 ▶ my $row = $self->columns || $self->fields;
Condition100%3/3
$self->columns || $self->fields
l!l&&r!l&&!r
2131
MC/DC100%2/2
$self->columns || $self->fields
$self->columns $self->fields
Truth table100%3/3
A || B
A= $self->columnsB= $self->fields
ABresult
1X1
011
000
71 16 ▶ $self->csv->print($self->fh, $row) if $row && @$row;
Branch: 1 branch
BranchTrueFalseTotal
if $row and @$row 15 1 16
Condition66%2/3
$row and @$row
!ll&&!rl&&r
1015
MC/DC50%1/2
$row and @$row
$row @$row
Truth table66%2/3
A and B
A= $rowB= @$row
ABresult
0X0
100
111
72 }
73 }
74  
75 7 1;
76  
77 __END__
« Catmandu/Cmd/count.pm ↑ Summary Catmandu/Exporter/Count.pm »