Skip to content

Commit

Permalink
Merge branch 'mickey/rs_support_non_scrolled_fetch'
Browse files Browse the repository at this point in the history
  • Loading branch information
xsawyerx committed May 26, 2014
2 parents f159133 + eb77a27 commit f25a2d2
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 38 deletions.
10 changes: 5 additions & 5 deletions examples/rev_deps-recursive.pl
Expand Up @@ -17,16 +17,16 @@ sub dig {

my $res = $mcpan->reverse_dependencies($dist);

for ( @{$res} ) {
while ( my $item = $res->next ) {
if ( $level ) {
printf "%s%s\n",
colored( '....' x $level, 'yellow' ),
$_->distribution;
$item->distribution;
} else {
printf "\n>> %s\n",
colored( $_->distribution, 'blue' );
colored( $item->distribution, 'blue' );
}

dig( $_->distribution, $level + 1 );
dig( $item->distribution, $level + 1 );
}
};
}
15 changes: 8 additions & 7 deletions examples/rev_deps.pl
Expand Up @@ -6,12 +6,13 @@
my $deps =
MetaCPAN::Client->new->rev_deps('Hijk');

my @output = (
map +{
name => $_->name,
author => $_->author,
},
@{$deps},
);
my @output;

while ( my $rel = $deps->next ) {
push @output => {
name => $rel->name,
author => $rel->author,
};
}

p @output;
8 changes: 4 additions & 4 deletions lib/MetaCPAN/Client.pm
Expand Up @@ -208,10 +208,10 @@ sub _reverse_deps {
return [];
};

return +[
map { MetaCPAN::Client::Release->new_from_request($_->{'_source'}) }
@{ $res->{'hits'}{'hits'} }
];
return MetaCPAN::Client::ResultSet->new(
items => $res->{'hits'}{'hits'},
type => 'release',
);
}


Expand Down
65 changes: 44 additions & 21 deletions lib/MetaCPAN/Client/ResultSet.pm
Expand Up @@ -6,16 +6,6 @@ package MetaCPAN::Client::ResultSet;
use Moo;
use Carp;

has scroller => (
is => 'ro',
isa => sub {
ref $_[0] eq 'Search::Elasticsearch::Scroll'
or croak 'scroller must be an Search::Elasticsearch::Scroll object';
},
handles => ['total'],
required => 1,
);

has type => (
is => 'ro',
isa => sub {
Expand All @@ -26,31 +16,64 @@ has type => (
required => 1,
);

has facets => (
# in case we're returning from a scrolled search
has scroller => (
is => 'ro',
isa => sub {
use Safe::Isa;
$_[0]->$_isa('Search::Elasticsearch::Scroll')
or croak 'scroller must be an Search::Elasticsearch::Scroll object';
},
predicate => 'has_scroller',
);

# in case we're returning from a fetch
has items => (
is => 'ro',
isa => sub {
ref $_[0] eq 'ARRAY'
or croak 'items must be an array ref';
},
);

has total => (
is => 'ro',
lazy => 1,
builder => '_get_facets',
default => sub {
my $self = shift;

return $self->has_scroller ? $self->scroller->total
: scalar @{ $self->items };
},
);

sub _get_facets {
my $self = shift;
sub BUILDARGS {
my ( $class, %args ) = @_;

return $self->scroller->facets || {};
}
exists $args{scroller} or exists $args{items}
or croak 'ResultSet must get either scroller or items';

exists $args{scroller} and exists $args{items}
and croak 'ResultSet must get either scroller or items, not both';

return \%args;
}

sub next {
my $self = shift;
my $result = $self->scroller->next;
my $result = $self->has_scroller ? $self->scroller->next
: shift @{ $self->items };

defined $result
or return;
defined $result or return;

my $class = 'MetaCPAN::Client::' . ucfirst $self->type;

return $class->new_from_request( $result->{'_source'} || $result->{'fields'} );
}

sub facets {
my $self = shift;

return $self->has_scroller ? $self->scroller->facets : {};
}

1;

Expand Down
8 changes: 7 additions & 1 deletion t/resultset.t
Expand Up @@ -7,11 +7,17 @@ use Test::More tests => 3;
use Test::Fatal;
use MetaCPAN::Client::ResultSet;

{
package MetaCPAN::Client::Test::ScrollerZ;
use base 'Search::Elasticsearch::Scroll'; # < 5.10 FTW (except, no)
sub total {0}
}

like(
exception {
MetaCPAN::Client::ResultSet->new(
type => 'failZZ',
scroller => bless {}, 'Search::Elasticsearch::Scroll',
scroller => bless {}, 'MetaCPAN::Client::Test::ScrollerZ',
)
},
qr/Invalid type/,
Expand Down

0 comments on commit f25a2d2

Please sign in to comment.