File Coverage

blib/lib/Data/SearchEngine.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Data::SearchEngine;
2             {
3             $Data::SearchEngine::VERSION = '0.33';
4             }
5 2     2   58912 use Moose::Role;
  0            
  0            
6              
7             # ABSTRACT: A role for search engine abstraction.
8              
9             requires qw(find_by_id search);
10              
11              
12              
13             has debug => (
14             is => 'rw',
15             isa => 'Any',
16             predicate => 'is_debug'
17             );
18              
19              
20             has defaults => (
21             traits => [ 'Hash' ],
22             is => 'ro',
23             isa => 'HashRef',
24             handles => {
25             set_default => 'set',
26             get_default => 'get'
27             }
28             );
29              
30              
31             no Moose::Role;
32             1;
33              
34             __END__
35             =pod
36              
37             =head1 NAME
38              
39             Data::SearchEngine - A role for search engine abstraction.
40              
41             =head1 VERSION
42              
43             version 0.33
44              
45             =head1 SYNOPSIS
46              
47             package Data::SearchEngine::MySearch;
48             use Moose;
49              
50             with 'Data::SearchEngine';
51              
52             sub search {
53             my ($self, $query) = @_;
54              
55             # ... your internal search junk
56              
57             my $result = Data::SearchEngine::Results->new(
58             query => $query,
59             pager => # ... make a Data::Page
60             );
61              
62             my @hits; # Populate with hits somehow
63              
64             foreach my $hit (@hits) {
65             $result->add(Data::SearchEngine::Item->new(
66             values => {
67             name => $hit->name,
68             description => $hit->description
69             },
70             score => $hit->score
71             ));
72             }
73              
74             return $result;
75             }
76              
77             =head1 DESCRIPTION
78              
79             There are B<lots> of search engine libraries. Each has a different interface.
80             The goal of Data::SearchEngine is to provide a simple, extensive set of
81             classes and roles that you can use to wrap a search implementation. The net
82             result will be an easily swappable backend with a common set of features.
83              
84             =head1 IMPLEMENTATION
85              
86             B<NOTE:> You should avoid adding new attributes or subclassing
87             Data::SearchEngine classes unless otherwise noted. Doing so will break
88             compatability with future releases and obviate the whole reason that
89             Data::SearchEngine exists. The only exception is the Results class (step 3
90             below) which should B<only> be a subclass with roles applied, no new attributes
91             or methods.
92              
93             =head2 Step 1 - Wrap a search implementation
94              
95             As shown in the SYNOPSIS, use the L<Data::SearchEngine> role in a class that
96             wraps your search implementation. Implement a C<search> method that takes a
97             L<Data::SearchEngine::Query> object and returns a
98             L<Data::SearchEngine::Results> object.
99              
100             =head2 Step 2 - Use Other Roles
101              
102             If your library includes functionality other than searching, such as indexing
103             new documents or removing them, you may include the
104             L<Data::SearchEngine::Modifiable> role. If you have other suggestions for
105             roles please drop me a line!
106              
107             =head2 Step 3 - Extend the Results
108              
109             The results object may not have quite enough pieces for your implementation.
110             If not, you can C<extend> L<Data::SearchEngine::Results> and add some other
111             roles:
112              
113             =over 4
114              
115             =item Data::SearchEngine::Results::Faceted
116              
117             For results that contain faceted data.
118              
119             =item Data::SearchEngine::Results::Spellcheck
120              
121             For results that contain spellcheck data.
122              
123             =back
124              
125             =head1 DIGESTS
126              
127             Data::SearchEngine provides a Digestable trait that can be applied to
128             attributes of C<Query>. Attributes with this trait will be added to
129             a base64 MD5 digest to produce a unique key identifying this query. You can
130             then serialize the Result using L<MooseX::Storage> and store it under the
131             digest of the Query for caching.
132              
133             =head1 ATTRIBUTES
134              
135             =head2 debug
136              
137             An attribute that signals the backend should operate in a debugging mode.
138             Please see the implementation module for specifics on how to use this.
139              
140             =head2 defaults
141              
142             The C<defaults> attribute is a simple HashRef that backends may use to get
143             default settings from the user. The implementation of C<search> may then use
144             these defaults when setting up instances of a search.
145              
146             =head1 METHODS
147              
148             =head2 is_debug
149              
150             Method for determining if the debug attribute has been set.
151              
152             =head2 get_default ($key)
153              
154             Returns the value from C<defaults> (if any) for the specified key.
155              
156             =head2 set_default ($key, $value)
157              
158             Sets the value in C<defaults>.
159              
160             =head1 AUTHOR
161              
162             Cory G Watson <gphat@cpan.org>
163              
164             =head1 COPYRIGHT AND LICENSE
165              
166             This software is copyright (c) 2012 by Cold Hard Code, LLC.
167              
168             This is free software; you can redistribute it and/or modify it under
169             the same terms as the Perl 5 programming language system itself.
170              
171             =cut
172