File Coverage

blib/lib/WebService/Lucene/Field.pm
Criterion Covered Total %
statement 29 29 100.0
branch 4 4 100.0
condition 4 5 80.0
subroutine 15 15 100.0
pod 11 11 100.0
total 63 64 98.4


line stmt bran cond sub pod time code
1             package WebService::Lucene::Field;
2              
3 3     3   8512 use strict;
  3         7  
  3         121  
4 3     3   17 use warnings;
  3         5  
  3         109  
5              
6 3     3   16 use base qw( Class::Accessor::Fast );
  3         7  
  3         5193  
7              
8             my %info = (
9             text => {
10             stored => 1,
11             indexed => 1,
12             tokenized => 1
13             },
14             keyword => {
15             stored => 1,
16             indexed => 1,
17             tokenized => 0
18             },
19             unindexed => {
20             stored => 1,
21             indexed => 0,
22             tokenized => 0
23             },
24             unstored => {
25             stored => 0,
26             indexed => 1,
27             tokenized => 1
28             },
29             sorted => {
30             stored => 0,
31             indexed => 1,
32             tokenized => 0
33             }
34             );
35              
36             __PACKAGE__->mk_accessors( qw( name value type ) );
37              
38             =head1 NAME
39              
40             WebService::Lucene::Field - Object to represent a field in a document
41              
42             =head1 SYNOPSIS
43              
44             $field = WebService::Lucene::Field->new( {
45             name => 'foo',
46             value => 'bar',
47             type => 'text'
48             } );
49            
50             # or via the 'text' method
51             $field = WebService::Lucene::Field->text(
52             name => 'foo',
53             value => 'bar'
54             );
55              
56             =head1 DESCRIPTION
57              
58             =head1 METHODS
59              
60             =head2 new( $options )
61              
62             Creates a new field object from the options provided.
63              
64             =head2 types( )
65              
66             Returns the types of fields available.
67              
68             =cut
69              
70             sub types {
71 1     1 1 1461 return keys %info;
72             }
73              
74             =head2 text( $name => $value )
75              
76             Create a new text field.
77              
78             =cut
79              
80             sub text {
81 1     1 1 849 return shift->_new_as( 'text', @_ );
82             }
83              
84             =head2 keyword( $name => $value )
85              
86             Create a new keyword field.
87              
88             =cut
89              
90             sub keyword {
91 1     1 1 822 return shift->_new_as( 'keyword', @_ );
92             }
93              
94             =head2 unindexed( $name => $value )
95              
96             Creates a new unindexed field.
97              
98             =cut
99              
100             sub unindexed {
101 1     1 1 8335 return shift->_new_as( 'unindexed', @_ );
102             }
103              
104             =head2 unstored( $name => $value )
105              
106             Creates a new unstored field.
107              
108             =cut
109              
110             sub unstored {
111 1     1 1 803 return shift->_new_as( 'unstored', @_ );
112             }
113              
114             =head2 sorted( $name => $value )
115              
116             Creates a new sorted field.
117              
118             =cut
119              
120             sub sorted {
121 1     1 1 775 return shift->_new_as( 'sorted', @_ );
122             }
123              
124             =head2 _new_as( $type, $name => $value )
125              
126             A shorter way to generate a field object.
127              
128             =cut
129              
130             sub _new_as {
131 5     5   48 return shift->new( { type => shift, name => shift, value => shift } );
132             }
133              
134             =head2 is_stored( )
135              
136             Will the field be stored in the index?
137              
138             =cut
139              
140             sub is_stored {
141 10     10 1 4542 return $info{ shift->type }->{ stored };
142             }
143              
144             =head2 is_indexed( )
145              
146             Will the field be indexed?
147              
148             =cut
149              
150             sub is_indexed {
151 10     10 1 104791 return $info{ shift->type }->{ indexed };
152             }
153              
154             =head2 is_tokenized( )
155              
156             Will the field be tokenized in the index?
157              
158             =cut
159              
160             sub is_tokenized {
161 10     10 1 5500 return $info{ shift->type }->{ tokenized };
162             }
163              
164             =head2 get_info( [$type] )
165              
166             Returns a hashref of info for the current or specified type.
167              
168             =cut
169              
170             sub get_info {
171 15     15 1 12618 my ( $self, $type ) = @_;
172              
173 15   66     75 $type ||= $self->type;
174              
175 15         155 return $info{ $type };
176             }
177              
178             =head2 get_type( $info )
179              
180             Given a hashref of information (stored, indexed, tokenzied)
181             it will return the type of field.
182              
183             =cut
184              
185             sub get_type {
186 5     5 1 13600 my ( $class, $args ) = @_;
187              
188 5         17 for my $type ( keys %info ) {
189 15         19 my $data = $info{ $type };
190 15         20 my $match = 1;
191              
192 15         46 for ( keys %$data ) {
193 45 100 100     187 $match = 0 && last
194             unless !( $data->{ $_ } ^ ( $args->{ $_ } || 0 ) );
195             }
196              
197 15 100       61 return $type if $match;
198             }
199             }
200              
201             =head2 name( [$name] )
202              
203             Accessor for the field name.
204              
205             =head2 value( [$value] )
206              
207             Accessor for the field value.
208              
209             =head2 type( [$type] )
210              
211             Accessor for the field type.
212              
213             =cut
214              
215             =head1 AUTHORS
216              
217             =over 4
218              
219             =item * Brian Cassidy Ebrian.cassidy@nald.caE
220              
221             =item * Adam Paynter Eadam.paynter@nald.caE
222              
223             =back
224              
225             =head1 COPYRIGHT AND LICENSE
226              
227             Copyright 2006-2009 National Adult Literacy Database
228              
229             This library is free software; you can redistribute it and/or modify
230             it under the same terms as Perl itself.
231              
232             =cut
233              
234             1;