File Coverage

blib/lib/Plucene/Plugin/FileDocument.pm
Criterion Covered Total %
statement 26 26 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 37 37 100.0


line stmt bran cond sub pod time code
1             package Plucene::Plugin::FileDocument;
2              
3             our $VERSION = '1.01';
4              
5 1     1   25453 use strict;
  1         3  
  1         48  
6 1     1   6 use warnings;
  1         1  
  1         41  
7              
8             =head1 NAME
9              
10             Plucene::Plugin::FileDocument - turn a file into a document
11              
12             =head1 SYNOPSIS
13              
14             my $writer = Plucene::Index::Writer->new(...);
15             $writer->add_document(Plucene::Plugin::FileDocument->new($filename));
16              
17             =head1 DESCRIPTION
18              
19             This is a Plucene::Document subclass for indexing an entire file on disk.
20              
21             =head1 CONSTRUCTOR
22              
23             =head2 new
24              
25             $writer->add_document(Plucene::Plugin::FileDocument->new($filename));
26              
27             This loads the file with the given name, and turns it into a
28             Plucene::Document.
29              
30             By default two Text fields will be created: 'path', which is the full
31             name of the file, and 'text' which is the content of the file.
32              
33             Extra fields can can be added by passing a hash of { fieldname => subref }
34             pairs to the constructor:
35              
36             $writer->add_document(Plucene::Plugin::FileDocument->new($filename,
37             lastmod => sub { (stat(+shift->{path}))[9] },
38             );
39              
40             These subrefs will be passed a hashref of the 'path' and 'text'.
41              
42             =head1 CUSTOMISATION
43              
44             =head2 document_class / field_class
45              
46             By default we assume you are using Plucene::Document and
47             Plucene::Document::Field. If you are using subclasses of these instead,
48             then you should override these methods accordingly.
49              
50             =cut
51              
52 1     1   940 use File::Slurp 'read_file';
  1         18116  
  1         73  
53              
54 1     1   963 use Plucene::Document;
  1         5333  
  1         40  
55 1     1   936 use Plucene::Document::Field;
  1         769  
  1         9  
56              
57             sub new {
58 2     2 1 5980 my ($self, $file, %args) = @_;
59 2         9 my $doc = $self->document_class->new;
60 2         32 my $field_class = $self->field_class;
61 2         12 my $hash = {
62             path => $file,
63             text => scalar read_file($file),
64             };
65 2         405 while (my ($field, $subref) = each %args) {
66 1         4 $hash->{$field} = $subref->($hash);
67             }
68 2         38 while (my ($key, $val) = each %$hash) {
69 5         153 $doc->add($field_class->Text($key => $val));
70             }
71 2         67 return $doc;
72             }
73              
74 2     2 1 24 sub document_class { "Plucene::Document" }
75 2     2 1 5 sub field_class { "Plucene::Document::Field" }
76              
77             =head1 AUTHOR
78              
79             Tony Bowden, based on example code from the Lucene benchmark suite.
80              
81             =head1 BUGS and QUERIES
82              
83             Please direct all correspondence regarding this module to:
84             bug-Plucene-Plugin-FileDocument@rt.cpan.org
85              
86             =head1 COPYRIGHT AND LICENSE
87              
88             Copyright (C) 2004-2005 Kasei
89              
90             This program is free software; you can redistribute it and/or modify
91             it under the terms of the GNU General Public License; either version
92             2 of the License, or (at your option) any later version.
93              
94             This program is distributed in the hope that it will be useful,
95             but WITHOUT ANY WARRANTY; without even the implied warranty of
96             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
97              
98             =cut
99              
100             1;
101