File Coverage

blib/lib/Net/Braintree/ResourceCollection.pm
Criterion Covered Total %
statement 3 33 9.0
branch 0 8 0.0
condition n/a
subroutine 1 8 12.5
pod 0 7 0.0
total 4 56 7.1


line stmt bran cond sub pod time code
1             package Net::Braintree::ResourceCollection;
2 1     1   6 use Moose;
  1         1  
  1         8  
3             extends "Net::Braintree::ResultObject";
4              
5             has 'response' => (is => 'rw');
6             has 'ids' => (is => 'rw');
7             has 'page_size' => (is => 'rw');
8             has 'callback' => (is => 'rw');
9              
10             sub init {
11 0     0 0   my ($self, $response, $callback) = @_;
12 0           $self->response($response);
13 0           $self->ids($response->{search_results}->{ids});
14 0           $self->page_size($response->{search_results}->{page_size});
15 0           $self->callback($callback);
16 0           return $self;
17             }
18              
19             sub is_success {
20 0     0 0   my $self = shift;
21 0 0         return 1 unless $self->response->{'api_error_response'};
22 0           return 0;
23             }
24              
25             sub first {
26 0     0 0   my $self = shift;
27 0 0         return undef if $self->is_empty;
28 0           my $first_id = $self->ids->[0];
29 0           return $self->callback->([$first_id])->[0];
30             }
31              
32             sub is_empty {
33 0     0 0   my $self = shift;
34 0           $self->maximum_size == 0;
35             }
36              
37             sub maximum_size {
38 0     0 0   my $self = shift;
39 0           return (scalar @{$self->ids});
  0            
40             }
41              
42             sub each {
43 0     0 0   my ($self, $block) = @_;
44 0           my @page = ();
45              
46 0           for(my $count = 0; $count < $self->maximum_size; $count++) {
47 0           push(@page, $self->ids->[$count]);
48 0 0         if((scalar @page) == $self->page_size) {
49 0           $self->execute_block_for_page($block, @page);
50 0           @page = ();
51             }
52             }
53              
54 0 0         $self->execute_block_for_page($block, @page) if(scalar(@page) > 0);
55             }
56              
57             sub execute_block_for_page {
58 0     0 0   my ($self, $block, @page) = @_;
59 0           my $resources = $self->callback->([@page]);
60 0           for my $resource (@$resources) {
61 0           $block->($resource);
62             }
63             }
64              
65             1;