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   92365 use strict;
  21         39  
  21         666  
2 21     21   86 use warnings;
  21         31  
  21         1343  
3             package MetaCPAN::Client::ResultSet;
4             # ABSTRACT: A Result Set
5             $MetaCPAN::Client::ResultSet::VERSION = '2.043000';
6 21     21   511 use Moo;
  21         5721  
  21         108  
7 21     21   7180 use Carp;
  21         46  
  21         1689  
8              
9 21     21   477 use MetaCPAN::Client::Types qw< ArrayRef >;
  21         58  
  21         2939  
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   8886 use Safe::Isa;
  21         10860  
  21         11039  
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             );
37              
38             has total => (
39             is => 'ro',
40             default => sub {
41             my $self = shift;
42              
43             return $self->has_scroller ? $self->scroller->total
44             : scalar @{ $self->items };
45             },
46             );
47              
48             has 'class' => (
49             is => 'ro',
50             lazy => 1,
51             builder => '_build_class',
52             );
53              
54             sub BUILDARGS {
55 14     14 1 276835 my ( $class, %args ) = @_;
56              
57             exists $args{scroller} or exists $args{items}
58 14 50 66     78 or croak 'ResultSet must get either scroller or items';
59              
60             exists $args{scroller} and exists $args{items}
61 14 50 66     99 and croak 'ResultSet must get either scroller or items, not both';
62              
63             exists $args{index} or exists $args{class}
64 14 100 100     186 or croak 'Must pass either index or target class to ResultSet';
65              
66             exists $args{index} and exists $args{class}
67 13 50 66     78 and croak 'Must pass either index or target class to ResultSet, not both';
68              
69 13         232 return \%args;
70             }
71              
72             sub BUILD {
73 12     12 1 210 my ( $self ) = @_;
74 12         232 $self->class; # vivify and validate
75             }
76              
77             sub next {
78 46     46 1 36305 my $self = shift;
79             my $result = $self->has_scroller ? $self->scroller->next
80 46 100       186 : shift @{ $self->items };
  31         69  
81              
82 46 100       138 defined $result or return;
83              
84 43   33     734 return $self->class->new_from_request( $result->{'_source'} || $result->{'fields'} || $result );
85             }
86              
87             sub aggregations {
88 0     0 1 0 my $self = shift;
89              
90 0 0       0 return $self->has_scroller ? $self->scroller->aggregations : {};
91             }
92              
93             sub _build_class {
94 11     11   109 my $self = shift;
95 11         210 return 'MetaCPAN::Client::' . ucfirst $self->index;
96             }
97              
98             1;
99              
100             __END__