File Coverage

blib/lib/Clustericious/RouteBuilder/Search.pm
Criterion Covered Total %
statement 12 43 27.9
branch 0 22 0.0
condition 0 19 0.0
subroutine 4 6 66.6
pod n/a
total 16 90 17.7


line stmt bran cond sub pod time code
1             package Clustericious::RouteBuilder::Search;
2              
3 1     1   879 use strict;
  1         2  
  1         26  
4 1     1   4 use warnings;
  1         2  
  1         20  
5 1     1   5 use Clustericious::Log;
  1         2  
  1         8  
6              
7             # ABSTRACT: build routes for searching for objects
8             our $VERSION = '1.27'; # VERSION
9              
10              
11 1         10 use Sub::Exporter -setup => {
12             exports => [
13             "search" => \&_build_search,
14             ],
15             collectors => ['defaults'],
16 1     1   1026 };
  1         3370  
17              
18             sub _build_search {
19 0     0     my ($class, $name, $arg, $defaults) = @_;
20 0   0       my $finder = $arg->{finder} || $defaults->{defaults}{finder} || die "no finder defined";
21 0 0         $finder->can("find_class") or die "$finder must be able to find_class";
22             sub {
23 0     0     my $self = shift;
24              
25 0 0         my $items = $self->stash("items") or LOGDIE "no items in request";
26              
27 0 0         my $manager = $finder->find_class($items) or LOGDIE "no class for $items";
28 0 0         unless ($manager->can("object_class")) {
29             # Allow tables names in addition to plurals
30 0           $manager .= "::Manager";
31             }
32 0           $self->parse_autodata;
33              
34 0           my $p = $self->stash->{autodata};
35              
36 0 0         if (my $mode = $p->{mode}) {
37 0           DEBUG "Called search mode $mode";
38             # A search "mode" indicates that we use a pre-canned
39             # query stored in the manager class. This query is
40             # run by calling a method named "search_$mode" on the
41             # manager class. This method should return a count
42             # and a resultset (array ref of hashrefs).
43 0           my $method = "search_$mode";
44 0           my $got = $manager->$method($p);
45             ERROR "search_$mode did not return count/resultset"
46 0 0 0       unless ref($got) eq 'HASH' && exists($got->{count}) && exists($got->{resultset});
      0        
47 0           $self->stash(autodata => $got);
48 0           return;
49             }
50              
51              
52             #TRACE "searching for $items : ".Dumper($p);
53              
54             # maybe restrict, by first calling $manager->normalize_get_objects_args(%$p)
55              
56 0           my $all = delete $p->{query_all};
57             # "If the first argument is a hash it is treated as 'query'" -- RDBOM docs
58 0 0 0       my @args = $all || exists( $p->{query} ) ? %$p
    0          
59             : ( keys %$p > 0 ) ? $p
60             : ();
61 0           TRACE "args are @args";
62 0           push @args, object_class => $manager->object_class;
63 0           my %a = @args;
64 0 0 0       if (!$a{limit} && !$a{page}) {
    0 0        
65 0           DEBUG "Adding limit 100 to query";
66 0           push @args, ( limit => 100 );
67             } elsif ( ($a{limit} || $a{per_page} || 0) > 1000) {
68 0   0       WARN "Very large limit : ".($a{limit} || $a{per_page});
69             }
70              
71 0           my $count = $manager->get_objects_count( @args );
72 0           my @key = $manager->object_class->meta->primary_key_column_names;
73             $self->stash(autodata => {
74             count => $count,
75             key => (@key > 1 ? \@key : $key[0]),
76 0 0         resultset => [ map $_->as_hash, @{ $manager->get_objects( @args ) } ]
  0            
77             });
78 0           };
79             }
80              
81             1;
82              
83             __END__
84              
85             =pod
86              
87             =encoding UTF-8
88              
89             =head1 NAME
90              
91             Clustericious::RouteBuilder::Search - build routes for searching for objects
92              
93             =head1 VERSION
94              
95             version 1.27
96              
97             =head1 SYNOPSIS
98              
99             use My::Object::Class;
100             use Clustericious::RouteBuilder;
101             use Clustericious::RouteBuilder::Search
102             "search",
103             "plurals",
104             defaults => { manager_finder => "Manager::Finder::Class" },
105             ;
106              
107             ...
108              
109             post => "/:plural/search" => [ plural => [ plurals() ] ] => \&do_create;
110              
111             =head1 DESCRIPTION
112              
113             This automates the creation of routes for searching for objects.
114              
115             Manager::Finder::Class must provide the following methods :
116              
117             =over 4
118              
119             =item lookup_class
120              
121             given the plural of a table, look up the name of the class
122              
123             =back
124              
125             The route that is created turns a JSON structure which is input as POST
126             data into parameters for Rose::DB::Object::Manager::get_objects.
127              
128             Additionally a "mode" parameters is supported, which just calls a
129             search_$mode method within the manager class, and returns that
130             result set to the client.
131              
132             =head1 SUPER CLASS
133              
134             none
135              
136             =head1 SEE ALSO
137              
138             L<Clustericious>
139              
140             =head1 AUTHOR
141              
142             Original author: Brian Duggan
143              
144             Current maintainer: Graham Ollis E<lt>plicease@cpan.orgE<gt>
145              
146             Contributors:
147              
148             Curt Tilmes
149              
150             Yanick Champoux
151              
152             =head1 COPYRIGHT AND LICENSE
153              
154             This software is copyright (c) 2013 by NASA GSFC.
155              
156             This is free software; you can redistribute it and/or modify it under
157             the same terms as the Perl 5 programming language system itself.
158              
159             =cut