blib/lib/Mojo/CSV.pm

filter: Statementstmt 98.7% 78 / 79 Branchbran 72.2% 26 / 36 Conditioncond 55.5% 5 / 9 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 19 / 19 Podpod 100% 7 / 7 totaltotal 88.3% 136 / 154 CC 24 SCAR 15.7
CC
24
Cov
88%
text
10.4
__get_fh
7.3
trickle
4.1
CRAP 4.8
↑ Summary
1 package Mojo::CSV;
2  
3 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mojo::Collection qw/c/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use MooX::ChainedAttributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '1.001005'; # VERSION
14  
15 has in => (
16 is => 'rw',
17 chained => 1,
18 8 ▶ coerce => sub { __get_fh( $_[0], '<' ); }
Subroutine __ANON__: called CC=1 SCAR=0.0
19 2 );
20 has out => (
21 is => 'rw',
22 chained => 1,
23 3 ▶ coerce => sub { __get_fh( $_[0], '>' ); }
Subroutine __ANON__: called CC=1 SCAR=0.0
24 2 );
25 has _obj => (
26 is => 'ro',
27 13 ▶ default => sub { Text::CSV->new({ binary => 1 }) },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 2 );
29  
30 11 ▶sub __get_fh {
Subroutine __get_fh: called CC=7 SCAR=19.9
31 11 my ( $what, $how ) = @_;;
32  
33 11 my $fh;
34 11 ▶ if ( ref $what eq 'Mojo::Asset::File' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $what eq 'Mojo::Asset::File') { } 1 10 11
elsif (ref $what eq 'Mojo::Asset::Memory') { } 3 7 10
elsif (ref $what) { } 1 6 7
elsif (defined $what) { } 6 0 6
35 1 $fh = $what->handle;
36 }
37 elsif ( ref $what eq 'Mojo::Asset::Memory' ) {
38 3 ▶ open $fh, $how, \ $what->slurp or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $how, \$what->slurp or die $! 3 0 3
39 }
40 elsif ( ref $what ) {
41 1 $fh = $what;
42 }
43 elsif ( defined $what ) {
44 6 ▶ open $fh, $how, $what or croak "Failed to open CSV file ($what): $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $how, $what or croak("Failed to open CSV file ($what): $!") 6 0 6
45 }
46 else {
47 0 croak 'Could not figure out how to access resource';
48 }
49  
50 11 return $fh;
51 }
52  
53 4 ▶sub flush {
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
54 4 my $self = shift;
55 4 close $self->out;
56  
57 4 $self;
58 }
59  
60 64 ▶sub row {
Subroutine row: called CC=3 SCAR=11.0
Pod documented
61 64 my $self = shift;
62 64 ▶ my $in = $self->in or croak 'You must specify what to read from';
Branch: 1 branch
BranchTrueFalseTotal
my $in = $self->in or croak("You must specify what to read from") 63 1 64
63 63 ▶ my $row = $self->_obj->getline( $in ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $row = $self->_obj->getline($in) or return 56 7 63
64 56 return $row;
65 }
66  
67 6 ▶sub slurp {
Subroutine slurp: called CC=3 SCAR=11.0
Pod documented
68 6 my $self = shift;
69 6 ▶ @_ and $self->in( shift );
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->in(shift()) 6 0 6
70  
71 6 my @rows;
72 6 ▶ while ( my $r = $self->row ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $self->row) 48 6 54
73 48 push @rows, $r;
74 }
75  
76 6 return c @rows;
77 }
78  
79 2 ▶sub slurp_body {
Subroutine slurp_body: called CC=1 SCAR=0.0
Pod documented
80 2 my $self = shift;
81 2 return $self->slurp( @_ )->tail(-1);
82 }
83  
84 2 ▶sub spurt {
Subroutine spurt: called CC=3 SCAR=11.0
Pod documented
85 2 my $self = shift;
86 2 my $what = shift;
87 2 ▶ @_ and $self->out( shift );
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->out(shift()) 0 2 2
88 2 $self->trickle( $_ ) for @$what;
89 2 $self->flush;
90  
91 2 $self;
92 }
93  
94 9 ▶sub text {
Subroutine text: called CC=9 SCAR=23.4
Pod documented
95 9 my $self = shift;
96 9 ▶ my $what = shift or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $what = shift() or return "" 9 0 9
97  
98 9 my @out;
99 9 my $obj = $self->_obj;
100 9 ▶ if ( reftype $what
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($what) and builtin::reftype($what) eq 'ARRAY' and builtin::reftype($what->[0]) and builtin::reftype($what->[0]) eq 'ARRAY') { } 1 8 9
Condition55%5/9
builtin::reftype($what) and builtin::reftype($what) eq "ARRAY" and builtin::reftype($what->[0]) and builtin::reftype($what->[0]) eq "ARRAY"
!ll&&!rl&&r
801
builtin::reftype($what) and builtin::reftype($what) eq "ARRAY" and builtin::reftype($what->[0])
!ll&&!rl&&r
081
builtin::reftype($what) and builtin::reftype($what) eq "ARRAY"
!ll&&!rl&&r
009
MC/DC25%1/4
builtin::reftype($what) and builtin::reftype($what) eq "ARRAY" and builtin::reftype($what->[0]) and builtin::reftype($what->[0]) eq "ARRAY"
builtin::reftype($what) builtin::reftype($what) eq "ARRAY" builtin::reftype($what->[0]) builtin::reftype($what->[0]) eq "ARRAY"
Truth table40%2/5
A and B and C and D
A= builtin::reftype($what)B= builtin::reftype($what) eq "ARRAY"C= builtin::reftype($what->[0])D= builtin::reftype($what->[0]) eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
101 and reftype $what eq 'ARRAY'
102 and reftype $what->[0]
103 and reftype $what->[0] eq 'ARRAY'
104 ) {
105 1 for my $row ( @$what ) {
106 8 ▶ $obj->combine( @$row ) or next;
Branch: 1 branch
BranchTrueFalseTotal
$obj->combine(@$row) or next 8 0 8
107 8 push @out, $obj->string;
108 }
109 }
110 else {
111 8 ▶ $obj->combine( @$what ) and push @out, $obj->string;
Branch: 1 branch
BranchTrueFalseTotal
$obj->combine(@$what) and push @out, $obj->string 8 0 8
112 }
113  
114 9 return join "\n", @out;
115 }
116  
117 19 ▶sub trickle {
Subroutine trickle: called CC=4 SCAR=14.0
Pod documented
118 19 my $self = shift;
119 19 ▶ my $what = shift or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $what = shift() or return $self 19 0 19
120 19 ▶ my $out = $self->out or croak 'You must specify where to write';
Branch: 1 branch
BranchTrueFalseTotal
my $out = $self->out or croak("You must specify where to write") 18 1 19
121 18 my $obj = $self->_obj;
122 18 ▶ $obj->combine( @$what ) or return $self;
Branch: 1 branch
BranchTrueFalseTotal
$obj->combine(@$what) or return $self 18 0 18
123 18 print $out $obj->string . "\n";
124  
125 18 $self;
126 }
127  
128 2 q|
129 Computers are like air conditioners.
130 They work fine until you start opening windows.
131 |;
132  
133 __END__
↑ Summary