Prog/Aggregator/Object.pm

filter: Statementstmt 28.2% 28 / 99 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 61.5% 8 / 13 Podpod 100% 4 / 4 totaltotal 22.1% 40 / 181 CC 29 SCAR 42.3
CC
29
Cov
18%
init
156.0
crawl
72.0
get_doc
42.0
CRAP 68.9
« Prog/Config.pm ↑ Summary Prog/Class.pm »
1 package SWISH::Prog::Aggregator::Object;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw( SWISH::Prog::Aggregator );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use YAML::Syck ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use SWISH::Prog::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 __PACKAGE__->mk_accessors(
14 qw( methods class title url modtime serial_format ));
15  
16 1 our $VERSION = '0.75';
17  
18 1 my $XMLer = Search::Tools::XML->new(); # included in Utils
19  
20 =pod
21  
22 =head1 NAME
23  
24 SWISH::Prog::Aggregator::Object - index Perl objects with Swish-e
25  
26 =head1 SYNOPSIS
27
28 my $aggregator = SWISH::Prog::Aggregator::Object->new(
29 methods => [qw( foo bar something something_else )],
30 class => 'MyClass',
31 title => 'mytitle',
32 url => 'myurl',
33 modtime => 'mylastmod'
34 indexer => SWISH::Prog::Indexer::Native->new,
35 );
36
37 my $data = my_func_for_fetching_data();
38 # $data is either iterator or arrayref of objects
39
40 $aggregator->indexer->start;
41 $aggregator->crawl( $data );
42 $aggregator->indexer->finish;
43  
44 =head1 DESCRIPTION
45  
46 SWISH::Prog::Aggregator::Object is designed for providing full-text
47 search for your Perl objects with Swish-e.
48  
49 Since SWISH::Prog::Aggregator::Object inherits from SWISH::Prog::Aggregator,
50 read that documentation first. Any overridden methods are documented here.
51  
52 If it seems odd at first to think of indexing objects, consider the advantages:
53  
54 =over
55  
56 =item sorting
57  
58 Particularly for scalar method values, time for sorting objects by method
59 value is greatly decreased thanks to Swish-e's pre-sorted properties.
60  
61 =item SWISH::API::Object integration
62  
63 If you use SWISH::API::Object, you can get a Storable-like freeze/thaw effect with
64 SWISH::Prog::Aggregator::Object.
65  
66 =item caching
67  
68 If some methods in your objects take a long while to calculate values,
69 but don't change often, you can use Swish-e to cache those values,
70 similar to the Cache::* modules, but in a portable, fast index.
71  
72 =back
73  
74 =head1 METHODS
75  
76 =head2 new( I<opts> )
77  
78 Create new aggregator object.
79  
80 I<opts> may include:
81  
82 =over
83  
84 =item methods
85  
86 The B<methods> param takes an array ref of method names. Each method name
87 will be called on each object in crawl(). Each method name will also be stored
88 as a PropertyName in the Swish-e index, unless you explicitly create a
89 SWISH::Prog::Config object that that defines your PropertyNames.
90  
91 =item class
92  
93 The name of the class each object belongs to. The class value will be stored in the
94 index itself for later use with SWISH::API::Object (or for your own amusement).
95  
96 If not specified, the first object crawl()ed will be tested with the blessed()
97 function from Scalar::Util.
98  
99 =item title
100  
101 Which method to use as the B<swishtitle> value. Defaults to C<title>.
102  
103 =item url
104  
105 Which method to use as the B<swishdocpath> value. Defaults to C<url>.
106  
107 =item modtime
108  
109 Which method to use as the B<swishlastmodified> value. Defaults to Perl built-in
110 time().
111  
112 =item serial_format
113  
114 Which format to use in serialize(). Default is C<json>. You can also use C<yaml>.
115 If you don't like either of those, subclass SWISH::Prog::Aggregator::Object
116 and override serialize() to provide your own format.
117  
118 =back
119  
120 =head2 init
121  
122 Initialize object. This overrides SWISH::Prog::Aggregator init() base method.
123  
124 =cut
125  
126 0 ▶sub init {
Subroutine init: not called CC=12 SCAR=50.5
Pod documented
127 0 my $self = shift;
128 0 $self->SUPER::init(@_);
129  
130 0 ▶ $self->{title} ||= 'title';
Condition0%0/2
$self->{"title"} ||= "title"
l!l
00
MC/DC0%0/1
$self->{"title"} ||= "title"
$self->{"title"}
Truth table0%0/2
A
A= $self->{"title"}
Aresult
11
00
131 0 ▶ $self->{url} ||= 'url';
Condition0%0/2
$self->{"url"} ||= "url"
l!l
00
MC/DC0%0/1
$self->{"url"} ||= "url"
$self->{"url"}
Truth table0%0/2
A
A= $self->{"url"}
Aresult
11
00
132 0 ▶ $self->{modtime} ||= 'modtime';
Condition0%0/2
$self->{"modtime"} ||= "modtime"
l!l
00
MC/DC0%0/1
$self->{"modtime"} ||= "modtime"
$self->{"modtime"}
Truth table0%0/2
A
A= $self->{"modtime"}
Aresult
11
00
133 0 ▶ $self->{serial_format} ||= 'json';
Condition0%0/2
$self->{"serial_format"} ||= "json"
l!l
00
MC/DC0%0/1
$self->{"serial_format"} ||= "json"
$self->{"serial_format"}
Truth table0%0/2
A
A= $self->{"serial_format"}
Aresult
11
00
134  
135 0 ▶ unless ( $self->{methods} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"methods"}) 0 0 0
136 0 croak "methods required";
137 }
138  
139 # set up the config object
140 0 my $config = $self->{indexer}->{config};
141  
142 0 ( my $class_meta = $self->class ) =~ s/\W/\./g;
143 0 $self->{_class_meta} = $class_meta;
144  
145 # make urls find-able (really should adjust WordCharacters too...)
146 0 ▶ $config->MaxWordLimit(256) unless $config->MaxWordLimit;
Branch: 1 branch
BranchTrueFalseTotal
unless $config->MaxWordLimit 0 0 0
147  
148 # similar to DBI, we alias top-level tag
149 # so all words are find-able via swishdefault
150 0 ▶ $config->MetaNameAlias( 'swishdefault ' . $class_meta )
Branch: 1 branch
BranchTrueFalseTotal
unless $config->MetaNameAlias 0 0 0
151 unless $config->MetaNameAlias;
152 0 ▶ $config->MetaNames( @{ $self->methods } ) unless @{ $config->MetaNames };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$config->MetaNames;} 0 0 0
153  
154 0 $config->PropertyNames( @{ $self->methods } )
155 0 ▶ unless @{ $config->PropertyNames };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$config->PropertyNames;} 0 0 0
156  
157 # IMPORTANT to do this because whitespace matters in YAML
158 # NOTE that due to swish-e cache buffering, YAML fields
159 # that are longer than 10k can get seriously messed up.
160 # this is a swish-e bug that should be fixed.
161 0 $config->PropertyNamesNoStripChars( @{ $self->methods } )
162 0 ▶ unless @{ $config->PropertyNamesNoStripChars };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$config->PropertyNamesNoStripChars;} 0 0 0
163  
164 0 ▶ $config->IndexDescription(
Branch: 1 branch
BranchTrueFalseTotal
unless $config->IndexDescription 0 0 0
165 join(
166 ' ', 'class:' . $self->class, 'format:' . $self->serial_format
167 )
168 ) unless $config->IndexDescription;
169  
170 }
171  
172 =head2 crawl( I<data> )
173  
174 Index your objects.
175  
176 I<data> should either be an array ref of objects, or an iterator object with
177 a C<next> method. If I<data> is an iterator, it will be used like:
178  
179 while( my $object = $data->next )
180 {
181 $aggregator->method_to_index( $object );
182 }
183
184 Returns number of objects indexed.
185  
186 =cut
187  
188 0 ▶sub crawl {
Subroutine crawl: not called CC=8 SCAR=42.8
Pod documented
189 0 my $self = shift;
190 0 my $data = shift;
191 0 my $indexer = $self->indexer;
192  
193 # IMPORTANT! that this not be undef since url defaults to it.
194 0 $self->{count} = 0;
195  
196 0 ▶ if ( ref($data) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 0 0 0
elsif (ref $data and $data->can('next')) { } 0 0 0
197  
198 0 ▶ $self->{class} ||= blessed( $data->[0] );
Condition0%0/3
$self->{"class"} ||= builtin::blessed($data->[0])
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"class"} ||= builtin::blessed($data->[0])
$self->{"class"} builtin::blessed($data->[0])
Truth table0%0/3
A ||= B
A= $self->{"class"}B= builtin::blessed($data->[0])
ABresult
1X1
011
000
199 0 for my $o (@$data) {
200 0 $indexer->process( $self->get_doc($o) );
201 0 $self->_increment_count;
202 }
203  
204 }
205 ▶ elsif ( ref($data) && $data->can('next') ) {
Condition0%0/3
ref $data and $data->can("next")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $data and $data->can("next")
ref $data $data->can("next")
Truth table0%0/3
A and B
A= ref $dataB= $data->can("next")
ABresult
0X0
100
111
206 0 my $first = $data->next;
207 0 ▶ $self->{class} ||= blessed($first);
Condition0%0/3
$self->{"class"} ||= builtin::blessed($first)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"class"} ||= builtin::blessed($first)
$self->{"class"} builtin::blessed($first)
Truth table0%0/3
A ||= B
A= $self->{"class"}B= builtin::blessed($first)
ABresult
1X1
011
000
208 0 $indexer->process( $self->get_doc($first) );
209  
210 0 ▶ while ( my $o = $data->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = $data->next) 0 0 0
211 0 $indexer->process( $self->get_doc($o) );
212 0 $self->_increment_count;
213 }
214 }
215 else {
216 0 croak "\$data $data doesn't look like it's in the expected format";
217 }
218  
219 0 return $self->{count};
220 }
221  
222 =head2 get_doc( I<object> )
223  
224 Returns a doc_class() instance representing I<object>.
225  
226 =cut
227  
228 0 ▶sub get_doc {
Subroutine get_doc: not called CC=6 SCAR=37.4
Pod documented
229 0 my $self = shift;
230 0 ▶ my $object = shift or croak "need object";
Branch: 1 branch
BranchTrueFalseTotal
my $object = shift() or croak("need object") 0 0 0
231  
232 0 my $titlemeth = $self->{title};
233 0 my $urlmeth = $self->{url};
234 0 my $modtimemeth = $self->{modtime};
235  
236 0 ▶ my $title
Branch: 1 branch
BranchTrueFalseTotal
$object->can($titlemeth) ? : 0 0 0
237 = $object->can($titlemeth) ? $object->$titlemeth : '[ no title ]';
238  
239 my $url
240 = $object->can($urlmeth)
241 ? $object->$urlmeth
242 0 ▶ : $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
$object->can($urlmeth) ? : 0 0 0
243  
244 0 ▶ my $modtime
Branch: 1 branch
BranchTrueFalseTotal
$object->can($modtimemeth) ? : 0 0 0
245 = $object->can($modtimemeth)
246 ? $object->$modtimemeth
247 : time();
248  
249 0 my $xml = $self->_obj2xml( $self->{_class_meta}, $object, $title );
250  
251 0 my $doc = $self->doc_class->new(
252 content => $xml,
253 url => $url,
254 modtime => $modtime,
255 parser => 'XML*',
256 type => 'application/xml',
257 data => $object
258 );
259  
260 0 ▶ $self->debug and print $doc;
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and print $doc 0 0 0
261  
262 0 return $doc;
263 }
264  
265 0 ▶sub _obj2xml {
Subroutine _obj2xml: not called CC=3 SCAR=24.8
266 0 my ( $self, $class, $o, $title ) = @_;
267  
268 0 my $xml
269 = $XMLer->start_tag($class)
270 . "<swishtitle>"
271 . $XMLer->utf8_safe($title)
272 . "</swishtitle>";
273  
274 0 for my $m ( @{ $self->methods } ) {
275 0 my $v = $self->serialize( $o, $m );
276  
277 0 my @x = (
278 $XMLer->start_tag($m), $XMLer->utf8_safe($v), $XMLer->end_tag($m)
279 );
280  
281 0 $xml .= join( '', @x );
282 }
283 0 $xml .= $XMLer->end_tag($class);
284  
285 0 ▶ $self->debug and print STDOUT $xml . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and print STDOUT $xml . "\n" 0 0 0
286  
287 0 return $xml;
288 }
289  
290 =head2 serialize( I<object>, I<method_name> )
291  
292 Returns a serialized (stringified) version of the return value of I<method_name>.
293 If the return value is already a scalar string (i.e., if ref() returns false)
294 then the return value is returned untouched. Otherwise, the return value is serialized
295 with either JSON or YAML, depending on how you configured C<serial_format> in new().
296  
297 If you subclass SWISH::Prog::Aggregator::Object,
298 then you can (of course) return whatever serialized format you prefer.
299  
300 =cut
301  
302 0 ▶sub serialize {
Subroutine serialize: not called CC=4 SCAR=30.0
Pod documented
303 0 my $self = shift;
304 0 my ( $o, $m ) = @_;
305 0 my $v = $o->$m;
306 0 ▶ unless ( ref $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 0 0 0
307 0 return $v;
308 }
309 else {
310 0 ▶ if ( $self->serial_format eq 'json' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->serial_format eq 'json') { } 0 0 0
elsif ($self->serial_format eq 'yaml') { } 0 0 0
311 0 return JSON->new->convert_blessed(1)->allow_blessed(1)
312 ->encode($v);
313 }
314 elsif ( $self->serial_format eq 'yaml' ) {
315 0 return YAML::Syck::Dump($v);
316 }
317 else {
318 0 croak "unknown serial_format: " . $self->serial_format;
319 }
320 }
321  
322 }
323  
324 1 1;
325  
326 __END__
« Prog/Config.pm ↑ Summary Prog/Class.pm »