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