File Coverage

blib/lib/MetaCPAN/Client/ResultSet.pm
Criterion Covered Total %
statement 34 36 94.4
branch 10 16 62.5
condition 10 15 66.6
subroutine 10 11 90.9
pod 4 4 100.0
total 68 82 82.9


line stmt bran cond sub pod time code
1 21     21   137278 use strict;
  21         46  
  21         796  
2 21     21   100 use warnings;
  21         42  
  21         1557  
3             package MetaCPAN::Client::ResultSet;
4             # ABSTRACT: A Result Set
5             $MetaCPAN::Client::ResultSet::VERSION = '2.040000';
6 21     21   734 use Moo;
  21         8991  
  21         126  
7 21     21   9248 use Carp;
  21         60  
  21         1950  
8              
9 21     21   739 use MetaCPAN::Client::Types qw< ArrayRef >;
  21         52  
  21         3244  
10              
11             has type => (
12             is => 'ro',
13             isa => sub {
14             croak 'Invalid type' unless
15             grep { $_ eq $_[0] } qw
16             file module permission rating 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   9953 use Safe::Isa;
  21         13659  
  21         13465  
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 15     15 1 411431 my ( $class, %args ) = @_;
56              
57             exists $args{scroller} or exists $args{items}
58 15 50 66     105 or croak 'ResultSet must get either scroller or items';
59              
60             exists $args{scroller} and exists $args{items}
61 15 50 66     298 and croak 'ResultSet must get either scroller or items, not both';
62              
63             exists $args{type} or exists $args{class}
64 15 100 100     207 or croak 'Must pass either type or target class to ResultSet';
65              
66             exists $args{type} and exists $args{class}
67 14 50 66     99 and croak 'Must pass either type or target class to ResultSet, not both';
68              
69 14         369 return \%args;
70             }
71             sub BUILD {
72 13     13 1 191 my ( $self ) = @_;
73 13         392 $self->class; # vifify and validate
74             }
75              
76             sub next {
77 45     45 1 56150 my $self = shift;
78             my $result = $self->has_scroller ? $self->scroller->next
79 45 100       323 : shift @{ $self->items };
  31         112  
80              
81 45 100       155 defined $result or return;
82              
83 42   33     1233 return $self->class->new_from_request( $result->{'_source'} || $result->{'fields'} || $result );
84             }
85              
86             sub aggregations {
87 0     0 1 0 my $self = shift;
88              
89 0 0       0 return $self->has_scroller ? $self->scroller->aggregations : {};
90             }
91              
92             sub _build_class {
93 12     12   144 my $self = shift;
94 12 50       95 my $type = $self->type eq 'cve' ? 'CVE' : ( ucfirst $self->type );
95 12         288 return 'MetaCPAN::Client::' . $type;
96             }
97              
98             1;
99              
100             __END__