File Coverage

blib/lib/WWW/Ohloh/API/Collection.pm
Criterion Covered Total %
statement 52 60 86.6
branch 7 12 58.3
condition 4 8 50.0
subroutine 10 11 90.9
pod 1 3 33.3
total 74 94 78.7


line stmt bran cond sub pod time code
1             package WWW::Ohloh::API::Collection;
2             our $AUTHORITY = 'cpan:YANICK';
3              
4 32     32   279145 use strict;
  32         81  
  32         1480  
5 32     32   182 use warnings;
  32         61  
  32         1719  
6              
7 32     32   2172 use Object::InsideOut;
  32         72528  
  32         479  
8 32     32   5761 use Carp;
  32         1089  
  32         6644  
9              
10             our $VERSION = '1.0_1';
11              
12 32     32   275 use overload '<>' => \&next;
  32         164  
  32         344  
13              
14             my @cache_of : Field;
15             my @total_entries_of : Field : Default(-1);
16             my @page_of : Field;
17             my @max_entries_of : Field : Arg(max) : Get(max) : Default(undef);
18             my @element_of : Field : Arg(element) : Get(element);
19             my @sort_order_of : Field : Arg(sort);
20             my @query_of : Field : Arg(query);
21             my @ohloh_of : Field : Arg( name => 'ohloh', mandatory => 1);
22             my @read_so_far : Field : Get(get_read_so_far) : Set(set_read_so_far) :
23             Default(0);
24             my @all_read : Field;
25              
26             sub _init : Init {
27 0     0   0 my $self = shift;
28              
29 0         0 $cache_of[$$self] = []; # initialize to empty array
30 32     32   7921 }
  32         78  
  32         202  
31              
32             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33              
34             sub total_entries {
35 4     4 0 23 my $self = shift;
36              
37             # if not initialized, get a first bunch of entries
38 4 100       17 if ( $total_entries_of[$$self] == -1 ) {
39 1         5 $self->_gather_more;
40             }
41              
42 4         114 return $total_entries_of[$$self];
43             }
44              
45             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46              
47             sub next {
48 1     1 1 29 my $self = shift;
49 1   50     6 my $nbr_requested = shift || 1;
50              
51 1   66     2 while ( @{ $cache_of[$$self] } < $nbr_requested
  2         18  
52             and not $all_read[$$self] ) {
53 1         5 $self->_gather_more;
54             }
55              
56 1         2 my @bunch = splice @{ $cache_of[$$self] }, 0, $nbr_requested;
  1         2  
57              
58 1 50       4 if (@bunch) {
59 1 50       4 return wantarray ? @bunch : $bunch[0];
60             }
61              
62             # we've nothing else to return
63              
64 0         0 $page_of[$$self] = 0;
65 0         0 $all_read[$$self] = 0;
66              
67 0         0 return;
68             }
69              
70             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71              
72             sub _gather_more {
73 3     3   6 my $self = shift;
74              
75 3         17 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         40 my $class = $self->element;
82             my @new_batch =
83 3         9 map { $class->new( ohloh => $ohloh_of[$$self], xml => $_, ) }
  37         2467  
84             $xml->findnodes( $self->element_name );
85              
86 3 50 33     293 if ( defined( $self->max )
87             and $self->get_read_so_far + @new_batch > $self->max ) {
88 0         0 @new_batch =
89             @new_batch[ 0 .. $self->max - $self->get_read_so_far - 1 ];
90 0         0 $all_read[$$self] = 1;
91             }
92              
93 3         121 $read_so_far[$$self] += @new_batch;
94              
95 3 50       16 if ( @new_batch == 0 ) {
96 0         0 $all_read[$$self] = 1;
97             }
98              
99             # get total elements + where we are (but don't trust it)
100              
101 3         11 $total_entries_of[$$self] =
102             $xml->findvalue('/response/items_available/text()');
103              
104 3         179 push @{ $cache_of[$$self] }, @new_batch;
  3         13  
105              
106 3 50       19 $all_read[$$self] = 1 if $self->total_entries == $self->get_read_so_far;
107              
108 3         43 return;
109             }
110              
111             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112              
113             sub all {
114 1     1 0 844 my $self = shift;
115              
116 1         10 $self->_gather_more until ( $all_read[$$self] );
117              
118 1         42 my @bunch = @{ $cache_of[$$self] };
  1         5  
119 1         4 $cache_of[$$self] = [];
120              
121 1         3 $page_of[$$self] = 0;
122 1         3 $all_read[$$self] = 0;
123              
124 1         6 return @bunch;
125             }
126              
127             1;
128              
129             __END__