CouchLike/Iterator.pm

filter: Statementstmt 24.1% 15 / 62 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 40.0% 4 / 10 Podpod 0.0% 0 / 2 totaltotal 16.2% 19 / 117 CC 20 SCAR 40.8
CC
20
Cov
15%
_next_reduce
132.0
_next
30.0
all
20.0
CRAP 59.0
« CouchLike.pm ↑ Summary CouchLike/Pg.pm »
1 package DBIx::CouchLike::Iterator;
2  
3 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base qw/ Class::Accessor::Fast /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 __PACKAGE__->mk_accessors(qw/ couch sth query reduce /);
8  
9 1 my $Sub = {};
10  
11 0 ▶sub _next {
Subroutine _next: not called CC=5 SCAR=34.0
12 0 my $self = shift;
13 0 my $couch = $self->{couch};
14  
15 0 my $r = $self->{sth}->fetchrow_arrayref;
16 0 ▶ return unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
17  
18 0 ▶ my $res = {
Branch: 1 branch
BranchTrueFalseTotal
$r->[2] =~ /^[{\[]/ ? : 0 0 0
19 id => $r->[0],
20 key => $r->[1],
21 value => ( $r->[2] =~ /^[{\[]/ )
22 ? $couch->from_json($r->[2])
23 : $r->[2],
24 };
25 0 ▶ if ( $self->{query}->{include_docs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"query"}{"include_docs"}) 0 0 0
26 0 $res->{document} = $couch->from_json($r->[3]);
27 0 $res->{document}->{_id} = $r->[0];
28 }
29 0 ▶ delete $res->{key} unless defined $res->{key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res->{"key"} 0 0 0
30  
31 0 return $res;
32 }
33  
34 0 ▶sub next {
Subroutine next: not called CC=2 SCAR=17.9
Pod undocumented
35 0 my $self = shift;
36 0 ▶ return $self->{reduce} ? $self->_next_reduce()
Branch: 1 branch
BranchTrueFalseTotal
$self->{'reduce'} ? : 0 0 0
37 : $self->_next();
38 }
39  
40 0 ▶sub all {
Subroutine all: not called CC=4 SCAR=30.0
Pod undocumented
41 0 my $self = shift;
42 0 my @res;
43 0 ▶ if ( $self->{reduce} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'reduce'}) { } 0 0 0
44 0 ▶ push @res, $_ while $_ = $self->_next_reduce();
Branch: 1 branch
BranchTrueFalseTotal
$_ = $self->_next_reduce and do { push @res, $_ } 0 0 0
45 }
46 else {
47 0 ▶ push @res, $_ while $_ = $self->_next();
Branch: 1 branch
BranchTrueFalseTotal
$_ = $self->_next and do { push @res, $_ } 0 0 0
48 }
49 0 return @res;
50 }
51  
52 0 ▶sub _next_reduce {
Subroutine _next_reduce: not called CC=11 SCAR=48.8
53 0 my $self = shift;
54 0 my $sub_str = $self->{reduce};
55  
56 0 ▶ return if $self->{_exit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_exit"} 0 0 0
57  
58 0 ▶ my $sub = ( $Sub->{$sub_str} ||= eval $sub_str ); ## no critic
Condition0%0/3
$Sub->{$sub_str} ||= eval $sub_str
l!l&&r!l&&!r
000
MC/DC0%0/2
$Sub->{$sub_str} ||= eval $sub_str
$Sub->{$sub_str} eval $sub_str
Truth table0%0/3
A ||= B
A= $Sub->{$sub_str}B= eval $sub_str
ABresult
1X1
011
000
59 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
60 0 carp $@;
61 0 return;
62 }
63  
64 0 ▶ my $keys = $self->{_pre_key} || [];
Condition0%0/2
$self->{'_pre_key'} || []
l!l
00
MC/DC0%0/1
$self->{'_pre_key'} || []
$self->{'_pre_key'}
Truth table0%0/2
A
A= $self->{'_pre_key'}
Aresult
11
00
65 0 ▶ my $values = $self->{_pre_value} || [];
Condition0%0/2
$self->{'_pre_value'} || []
l!l
00
MC/DC0%0/1
$self->{'_pre_value'} || []
$self->{'_pre_value'}
Truth table0%0/2
A
A= $self->{'_pre_value'}
Aresult
11
00
66 0 ▶ my $pre_key = $self->{_pre_key} ? $self->{_pre_key}->[0]->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_pre_key'} ? : 0 0 0
67 0 ▶ while ( my $r = $self->_next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $self->_next) 0 0 0
68 0 ▶ $pre_key = $r->{key} unless defined $pre_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pre_key 0 0 0
69 0 ▶ if ( $r->{key} eq $pre_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"key"} eq $pre_key) 0 0 0
70 # key が同じうちは貯めていく
71 0 push @$keys, [ $r->{key}, $r->{id} ];
72 0 push @$values, $r->{value};
73 0 $pre_key = $r->{key};
74 0 next;
75 }
76 # key が変わったら貯めていた分を return
77 0 $self->{_pre_key} = [ [ $r->{key}, $r->{id} ] ];
78 0 $self->{_pre_value} = [ $r->{value} ];
79 0 return $self->_do_reduce( $sub, $keys, $values );
80 }
81  
82 0 $self->{_exit} = 1;
83 0 ▶ return unless defined $keys->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keys->[0] 0 0 0
84  
85 # 最後の
86 0 return $self->_do_reduce( $sub, $keys, $values );
87 }
88  
89 0 ▶sub _do_reduce {
Subroutine _do_reduce: not called CC=1 SCAR=6.9
90 0 my $self = shift;
91 0 my ( $sub, $keys, $values ) = @_;
92 return {
93 key => $keys->[0]->[0],
94 0 value => eval { $sub->( $keys, $values ) },
95 };
96 }
97  
98 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
99 0 my $self = shift;
100 0 ▶ $self->{sth}->finish() if $self->{sth};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sth"} 0 0 0
101 }
102  
103 1 1;
104  
« CouchLike.pm ↑ Summary CouchLike/Pg.pm »