API/Collection.pm

filter: Statementstmt 91.7% 67 / 73 Branchbran 68.7% 11 / 16 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 11 / 11 Podpod 33.3% 1 / 3 totaltotal 81.9% 95 / 116 CC 12 SCAR 13.7
CC
12
Cov
85%
next
7.0
_gather_more
5.8
total_entries
2.0
CRAP 3.9
« API/Stack.pm ↑ Summary API/Messages.pm »
1 package WWW::Ohloh::API::Collection;
2 32 our $AUTHORITY = 'cpan:YANICK';
3  
4 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 32 ▶use Object::InsideOut;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 32 our $VERSION = '1.0_1';
11  
12 32 ▶use overload '<>' => \&next;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 32 my @cache_of : Field;
15 32 my @total_entries_of : Field : Default(-1);
16 32 my @page_of : Field;
17 32 my @max_entries_of : Field : Arg(max) : Get(max) : Default(undef);
18 32 my @element_of : Field : Arg(element) : Get(element);
19 32 my @sort_order_of : Field : Arg(sort);
20 32 my @query_of : Field : Arg(query);
21 32 my @ohloh_of : Field : Arg( name => 'ohloh', mandatory => 1);
22 32 my @read_so_far : Field : Get(get_read_so_far) : Set(set_read_so_far) :
23 Default(0);
24 32 my @all_read : Field;
25  
26 5 ▶sub _init : Init {
Subroutine _init: called CC=1 SCAR=0.0
27 5 my $self = shift;
28  
29 5 $cache_of[$$self] = []; # initialize to empty array
30 32 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33  
34 4 ▶sub total_entries {
Subroutine total_entries: called CC=2 SCAR=6.9
Pod undocumented
35 4 my $self = shift;
36  
37 # if not initialized, get a first bunch of entries
38 4 ▶ if ( $total_entries_of[$$self] == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_entries_of[$$self] == -1) 1 3 4
39 1 $self->_gather_more;
40 }
41  
42 4 return $total_entries_of[$$self];
43 }
44  
45 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46  
47 1 ▶sub next {
Subroutine next: called CC=6 SCAR=19.5
Pod documented
48 1 my $self = shift;
49 1 ▶ my $nbr_requested = shift || 1;
Condition50%1/2
shift() || 1
l!l
01
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
50  
51 1 ▶ while ( @{ $cache_of[$$self] } < $nbr_requested
Branch: 1 branch
BranchTrueFalseTotal
if (@{$cache_of[$$self];} < $nbr_requested and not $all_read[$$self]) 1 1 2
Condition66%2/3
@{$cache_of[$$self];} < $nbr_requested and not $all_read[$$self]
!ll&&!rl&&r
101
MC/DC50%1/2
@{$cache_of[$$self];} < $nbr_requested and not $all_read[$$self]
@{$cache_of[$$self];} < $nbr_requested not $all_read[$$self]
Truth table66%2/3
A and not(B)
A= @{$cache_of[$$self];} < $nbr_requestedB= not $all_read[$$self]
ABresult
0X0
100
111
52 and not $all_read[$$self] ) {
53 1 $self->_gather_more;
54 }
55  
56 1 my @bunch = splice @{ $cache_of[$$self] }, 0, $nbr_requested;
57  
58 1 ▶ if (@bunch) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bunch) 1 0 1
59 1 ▶ return wantarray ? @bunch : $bunch[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
60 }
61  
62 # we've nothing else to return
63  
64 0 $page_of[$$self] = 0;
65 0 $all_read[$$self] = 0;
66  
67 0 return;
68 }
69  
70 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71  
72 3 ▶sub _gather_more {
Subroutine _gather_more: called CC=5 SCAR=17.6
73 3 my $self = shift;
74  
75 3 my ( $url, $xml ) = $ohloh_of[$$self]->_query_server(
76 $self->query_path,
77 { ( query => $query_of[$$self] ) x !!$query_of[$$self],
78 ( sort => $sort_order_of[$$self] ) x !!$sort_order_of[$$self],
79 page => ++$page_of[$$self] } );
80  
81 3 my $class = $self->element;
82 my @new_batch =
83 3 map { $class->new( ohloh => $ohloh_of[$$self], xml => $_, ) }
84 $xml->findnodes( $self->element_name );
85  
86 3 ▶ if ( defined( $self->max )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->max and $self->get_read_so_far + @new_batch > $self->max) 0 3 3
Condition33%1/3
defined $self->max and $self->get_read_so_far + @new_batch > $self->max
!ll&&!rl&&r
300
MC/DC0%0/2
defined $self->max and $self->get_read_so_far + @new_batch > $self->max
defined $self->max $self->get_read_so_far + @new_batch > $self->max
Truth table33%1/3
A and B
A= defined $self->maxB= $self->get_read_so_far + @new_batch > $self->max
ABresult
0X0
100
111
87 and $self->get_read_so_far + @new_batch > $self->max ) {
88 0 @new_batch =
89 @new_batch[ 0 .. $self->max - $self->get_read_so_far - 1 ];
90 0 $all_read[$$self] = 1;
91 }
92  
93 3 $read_so_far[$$self] += @new_batch;
94  
95 3 ▶ if ( @new_batch == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@new_batch == 0) 0 3 3
96 0 $all_read[$$self] = 1;
97 }
98  
99 # get total elements + where we are (but don't trust it)
100  
101 3 $total_entries_of[$$self] =
102 $xml->findvalue('/response/items_available/text()');
103  
104 3 push @{ $cache_of[$$self] }, @new_batch;
105  
106 3 ▶ $all_read[$$self] = 1 if $self->total_entries == $self->get_read_so_far;
Branch: 1 branch
BranchTrueFalseTotal
if $self->total_entries == $self->get_read_so_far 3 0 3
107  
108 3 return;
109 }
110  
111 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112  
113 1 ▶sub all {
Subroutine all: called CC=2 SCAR=6.9
Pod undocumented
114 1 my $self = shift;
115  
116 1 ▶ $self->_gather_more until ( $all_read[$$self] );
Branch: 1 branch
BranchTrueFalseTotal
$all_read[$$self] or do { $self->_gather_more } 1 1 2
117  
118 1 my @bunch = @{ $cache_of[$$self] };
119 1 $cache_of[$$self] = [];
120  
121 1 $page_of[$$self] = 0;
122 1 $all_read[$$self] = 0;
123  
124 1 return @bunch;
125 }
126  
127 32 1;
128  
129 __END__
« API/Stack.pm ↑ Summary API/Messages.pm »