File Coverage

blib/lib/Dezi/Doc.pm
Criterion Covered Total %
statement 15 32 46.8
branch 0 10 0.0
condition n/a
subroutine 5 8 62.5
pod 3 3 100.0
total 23 53 43.4


line stmt bran cond sub pod time code
1             package Dezi::Doc;
2 2     2   10 use Moo;
  2         2  
  2         16  
3 2     2   591 use Types::Standard qw( Str Int Num HashRef );
  2         5  
  2         12  
4 2     2   1363 use Carp;
  2         5  
  2         105  
5 2     2   1760 use Search::Tools::XML;
  2         222613  
  2         105  
6 2     2   17 use namespace::autoclean;
  2         6  
  2         16  
7              
8             our $VERSION = '0.003004';
9              
10             has 'mime_type' => ( is => 'rw', isa => Str );
11             has 'summary' => ( is => 'rw', isa => Str );
12             has 'title' => ( is => 'rw', isa => Str );
13             has 'content' => ( is => 'rw', isa => Str );
14             has 'uri' => ( is => 'rw', isa => Str );
15             has 'mtime' => ( is => 'rw', isa => Int );
16             has 'size' => ( is => 'rw', isa => Int );
17             has 'score' => ( is => 'rw', isa => Num );
18             has '_fields' => ( is => 'rw', isa => HashRef );
19              
20             =pod
21              
22             =head1 NAME
23              
24             Dezi::Doc - a Dezi client document
25              
26             =head1 SYNOPSIS
27              
28             # add doc to the index
29             use Dezi::Doc;
30             my $html = "hello world";
31             my $doc = Dezi::Doc->new(
32             mime_type => 'text/html',
33             uri => 'foo/bar.html',
34             mtime => time(),
35             size => length $html,
36             content => $html,
37             );
38             $client->index( $doc );
39            
40             # construct a document with field/value pairs
41             my $doc2 = Dezi::Doc->new(
42             uri => 'auto/xml/magic',
43             );
44             $doc2->set_field('title' => 'ima dezi doc');
45             $doc2->set_field('body' => 'hello world!');
46             $client->index( $doc2 );
47            
48             # search results are also Dezi::Doc objects
49             for my $doc (@{ $response->results }) {
50             printf("hit: %s %s\n", $doc->score, $doc->uri);
51             }
52              
53             =head1 DESCRIPTION
54              
55             Dezi::Doc represents one document in a collection.
56              
57             =head1 METHODS
58              
59             =head2 new
60              
61             Create new object. Takes pairs of key/values where the keys are one of:
62              
63             =over
64              
65             =item mime_type
66              
67             Sometimes known as the content type. A MIME type indicates the kind
68             of document this is.
69              
70             =item uri
71              
72             The unique URI for the document.
73              
74             =item mtime
75              
76             Last modified time. Should be expressed in Epoch seconds.
77              
78             =item size
79              
80             Length in bytes.
81              
82             =item content
83              
84             The document's content.
85              
86             =back
87              
88             =cut
89              
90             =head2 score
91              
92             When returned from a Dezi::Response->results array,
93             the score attribute is the search ranking score.
94              
95             =head2 title
96              
97             When returned from a Dezi::Response->results array,
98             the title is the document's parsed title.
99              
100             B you cannot set the title of a doc object when
101             sending to the index. See set_field() instead.
102              
103             =head2 summary
104              
105             When returned from a Dezi::Response->results array,
106             the summary is the snipped and highlighted extract
107             from the document showing query terms in context.
108              
109             B you cannot set the summary of a doc object when
110             sending to the index. The summary is a result field only.
111             It typically represents all or snipped part of the
112             C field in the index.
113              
114             =cut
115              
116             =head2 as_string_ref
117              
118             Returns a scalar ref pointing at the Dezi::Doc serialized,
119             either the value of content() or a XML fragment representing
120             values set with set_field().
121              
122             =cut
123              
124             sub as_string_ref {
125 0     0 1   my $self = shift;
126 0 0         if ( exists $self->{_fields} ) {
127             my $xml
128 0           = Search::Tools::XML->perl_to_xml( $self->{_fields}, 'doc', 1 );
129 0           return \$xml;
130             }
131             else {
132 0           my $content = $self->content;
133 0           return \$content;
134             }
135             }
136              
137             =head2 get_field( I )
138              
139             Returns the value of I.
140              
141             =cut
142              
143             sub get_field {
144 0     0 1   my $self = shift;
145 0 0         my $name = shift or croak "field_name required";
146 0 0         if ( !exists $self->{_fields}->{$name} ) {
147 0           return undef;
148             }
149 0           return $self->{_fields}->{$name};
150             }
151              
152             =head2 set_field( I => I )
153              
154             Set the I for field I.
155              
156             This method also sets the mime_type() of the document object
157             to 'application/xml' since that is how as_string_ref() will
158             render the object.
159              
160             =cut
161              
162             sub set_field {
163 0     0 1   my $self = shift;
164 0 0         my $field = shift or croak "field_name required";
165 0           my $value = shift;
166 0 0         croak "value required" unless defined $value;
167 0           $self->{_fields}->{$field} = $value;
168 0           $self->mime_type('application/xml');
169             }
170              
171             1;
172              
173             __END__