File Coverage

blib/lib/RT/Client/REST/SearchResult.pm
Criterion Covered Total %
statement 30 30 100.0
branch 5 6 83.3
condition 2 5 40.0
subroutine 7 7 100.0
pod 3 3 100.0
total 47 51 92.1


line stmt bran cond sub pod time code
1             #!perl
2             # vim: softtabstop=4 tabstop=4 shiftwidth=4 ft=perl expandtab smarttab
3             # PODNAME: RT::Client::REST::SearchResult
4             # ABSTRACT: search results object.
5              
6 9     9   63 use strict;
  9         23  
  9         355  
7 9     9   47 use warnings;
  9         20  
  9         3080  
8              
9             package RT::Client::REST::SearchResult;
10             $RT::Client::REST::SearchResult::VERSION = '0.72';
11             sub new {
12 1     1 1 2046 my $class = shift;
13              
14 1         5 my %opts = @_;
15              
16 1   33     8 my $self = bless {}, ref($class) || $class;
17              
18             # FIXME: add validation.
19 1         7 $self->{_object} = $opts{object};
20 1   50     5 $self->{_ids} = $opts{ids} || [];
21              
22 1         7 return $self;
23             }
24              
25 1     1 1 419 sub count { scalar( @{ shift->{_ids} } ) }
  1         8  
26              
27             sub _retrieve {
28 18     18   132 my ( $self, $obj ) = @_;
29              
30 18 50       43 unless ( $obj->autoget ) {
31 18         34 $obj->retrieve;
32             }
33              
34 18         60 return $obj;
35             }
36              
37             sub get_iterator {
38 2     2 1 462 my $self = shift;
39 2         5 my @ids = @{ $self->{_ids} };
  2         7  
40 2         5 my $object = $self->{_object};
41              
42             return sub {
43 12 100   12   458 if (wantarray) {
    100          
44 2         6 my @tomap = @ids;
45 2         4 @ids = ();
46              
47 2         9 return map { $self->_retrieve( $object->($_) ) } @tomap;
  9         20  
48             }
49             elsif (@ids) {
50 9         30 return $self->_retrieve( $object->( shift(@ids) ) );
51             }
52             else {
53 1         3 return; # This signifies the end of the iterations
54             }
55 2         27 };
56             }
57              
58             1;
59              
60             __END__
61              
62             =pod
63              
64             =encoding UTF-8
65              
66             =head1 NAME
67              
68             RT::Client::REST::SearchResult - search results object.
69              
70             =head1 VERSION
71              
72             version 0.72
73              
74             =head1 SYNOPSIS
75              
76             my $iterator = $search->get_iterator;
77             my $count = $iterator->count;
78              
79             while (defined(my $obj = &$iterator)) {
80             # do something with the $obj
81             }
82              
83             =head1 DESCRIPTION
84              
85             This class is a representation of a search result. This is the type
86             of the object you get back when you call method C<search()> on
87             L<RT::Client::REST::Object>-derived objects. It makes it easy to
88             iterate over results and find out just how many there are.
89              
90             =head1 METHODS
91              
92             =over 4
93              
94             =item B<count>
95              
96             Returns the number of search results. This number will always be the
97             same unless you stick your fat dirty fingers into the object and abuse
98             it. This number is not affected by calls to C<get_iterator()>.
99              
100             =item B<get_iterator>
101              
102             Returns a reference to a subroutine which is used to iterate over the
103             results.
104              
105             Evaluating it in scalar context, returns the next object
106             or C<undef> if all the results have already been iterated over. Note
107             that for each object to be instantiated with correct values,
108             B<retrieve()> method is called on the object before returning it
109             to the caller.
110              
111             Evaluating the subroutine reference in list context returns a list
112             of all results fully instantiated. WARNING: this may be expensive,
113             as each object is issued B<retrieve()> method. Subsequent calls to
114             the iterator result in empty list.
115              
116             You may safely mix calling the iterator in scalar and list context. For
117             example:
118              
119             $iterator = $search->get_iterator;
120              
121             $first = &$iterator;
122             $second = &$iterator;
123             @the_rest = &$iterator;
124              
125             You can get as many iterators as you want -- they will not step on
126             each other's toes.
127              
128             =item B<new>
129              
130             You should not have to call it yourself, but just for the sake of
131             completeness, here are the arguments:
132              
133             my $search = RT::Client::REST::SearchResult->new(
134             ids => [1 .. 10],
135             object => sub { # Yup, that's a closure.
136             RT::Client::REST::Ticket->new(
137             id => shift,
138             rt => $rt,
139             );
140             },
141             );
142              
143             =back
144              
145             =head1 SEE ALSO
146              
147             L<RT::Client::REST::Object>, L<RT::Client::REST>.
148              
149             =head1 AUTHOR
150              
151             Dean Hamstead <dean@fragfest.com.au>
152              
153             =head1 COPYRIGHT AND LICENSE
154              
155             This software is copyright (c) 2023, 2020 by Dmitri Tikhonov.
156              
157             This is free software; you can redistribute it and/or modify it under
158             the same terms as the Perl 5 programming language system itself.
159              
160             =cut