File: Hits.pm

package info (click to toggle)
libcatmandu-perl 0.9206-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,768 kB
  • ctags: 512
  • sloc: perl: 7,798; makefile: 34
file content (104 lines) | stat: -rw-r--r-- 1,766 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package Catmandu::Hits;

use namespace::clean;
use Catmandu::Sane;
use Moo;

has start  => (is => 'ro', required => 1);
has limit  => (is => 'ro', required => 1);
has total  => (is => 'ro', required => 1);
has hits   => (is => 'ro', required => 1);

with 'Catmandu::Iterable';
with 'Catmandu::Paged';

sub size {
    scalar @{$_[0]->hits};
}

sub more {
    my $self = $_[0];
    $self->start + $self->limit < $self->total;
}

sub generator {
    my $self = $_[0];
    my $hits = $self->hits;
    my $i = 0;
    sub {
        $hits->[$i++];
    };
}

sub to_array {
    [@{$_[0]->hits}];
}

sub count {
    scalar @{$_[0]->hits};
}

sub each {
    my ($self, $cb) = @_;
    my $hits = $self->hits;
    for my $hit (@$hits) {
        $cb->($hit);
    }
    $self->count;
}

sub first {
    $_[0]->hits->[0];
}

1;

=head1 NAME

Catmandu::Hits - Iterable object that wraps Catmandu::Store search hits

=head1 SYNOPSIS

    my $store = Catmandu::Store::Solr->new();

    my $hits  = $store->bag->search(
		   query => 'dna' ,
	           start => 0 ,
		   limit => 100 ,
		   sort  => 'title desc',
                );

    # Every hits is an iterator...
    $hits->each(sub { ... });

    printf "Found %s $hits\n" , $hits->total;

    my $start = $hits->start;
    my $limit = $hits->limit;

    my $prev = $hits->previous_page;
    my $next = $hits->next_page;

=head1 METHODS

=head2 total

Returns the total number of hits matching the query.

=head2 start

Returns the start index for the search results.

=head2 limit

Returns the maximum number of search results returned.

=head2 more

Return true if there are more search results.

=head1 SEE ALSO

L<Catmandu::Iterable>, L<Catmandu::Bag>, L<Catmandu::Searchable>, L<Catmandu::Store>, L<Catmandu::Paged>

=cut