File Coverage

blib/lib/MetaCPAN/Client/ResultSet.pm
Criterion Covered Total %
statement 33 35 94.2
branch 9 14 64.2
condition 10 15 66.6
subroutine 10 11 90.9
pod 4 4 100.0
total 66 79 83.5


line stmt bran cond sub pod time code
1 21     21   105327 use strict;
  21         38  
  21         750  
2 21     21   87 use warnings;
  21         35  
  21         1336  
3             package MetaCPAN::Client::ResultSet;
4             # ABSTRACT: A Result Set
5             $MetaCPAN::Client::ResultSet::VERSION = '2.044000';
6 21     21   551 use Moo;
  21         5698  
  21         120  
7 21     21   7970 use Carp;
  21         41  
  21         1863  
8              
9 21     21   481 use MetaCPAN::Client::Types qw< ArrayRef >;
  21         50  
  21         3182  
10              
11             has index => (
12             is => 'ro',
13             isa => sub {
14             croak 'Invalid index' unless
15             grep { $_ eq $_[0] } qw
16             file module permission release mirror package>;
17             },
18             lazy => 1,
19             );
20              
21             # in case we're returning from a scrolled search
22             has scroller => (
23             is => 'ro',
24             isa => sub {
25 21     21   10936 use Safe::Isa;
  21         11192  
  21         12347  
26             $_[0]->$_isa('MetaCPAN::Client::Scroll')
27             or croak 'scroller must be an MetaCPAN::Client::Scroll object';
28             },
29             predicate => 'has_scroller',
30             );
31              
32             # in case we're returning from a fetch
33             has items => (
34             is => 'ro',
35             isa => ArrayRef,
36             lazy => 1,
37             default => sub {
38             my $self = shift;
39             return [] unless $self->has_scroller;
40              
41             my @items;
42             while ( defined( my $result = $self->scroller->next ) ) {
43             push @items, $result;
44             }
45             return \@items;
46             },
47             );
48              
49             has total => (
50             is => 'ro',
51             default => sub {
52             my $self = shift;
53              
54             return $self->has_scroller ? $self->scroller->total
55             : scalar @{ $self->items };
56             },
57             );
58              
59             has 'class' => (
60             is => 'ro',
61             lazy => 1,
62             builder => '_build_class',
63             );
64              
65             sub BUILDARGS {
66 15     15 1 282380 my ( $class, %args ) = @_;
67              
68             exists $args{scroller} or exists $args{items}
69 15 50 66     78 or croak 'ResultSet must get either scroller or items';
70              
71             exists $args{scroller} and exists $args{items}
72 15 50 66     105 and croak 'ResultSet must get either scroller or items, not both';
73              
74             exists $args{index} or exists $args{class}
75 15 100 100     282 or croak 'Must pass either index or target class to ResultSet';
76              
77             exists $args{index} and exists $args{class}
78 14 50 66     87 and croak 'Must pass either index or target class to ResultSet, not both';
79              
80 14         292 return \%args;
81             }
82              
83             sub BUILD {
84 13     13 1 119 my ( $self ) = @_;
85 13         304 $self->class; # vivify and validate
86             }
87              
88             sub next {
89 46     46 1 44606 my $self = shift;
90             my $result = $self->has_scroller ? $self->scroller->next
91 46 100       241 : shift @{ $self->items };
  31         606  
92              
93 46 100       301 defined $result or return;
94              
95 43   33     750 return $self->class->new_from_request( $result->{'_source'} || $result->{'fields'} || $result );
96             }
97              
98             sub aggregations {
99 0     0 1 0 my $self = shift;
100              
101 0 0       0 return $self->has_scroller ? $self->scroller->aggregations : {};
102             }
103              
104             sub _build_class {
105 12     12   112 my $self = shift;
106 12         235 return 'MetaCPAN::Client::' . ucfirst $self->index;
107             }
108              
109             1;
110              
111             __END__