--- trunk/Estraier.pm 2006/01/04 13:11:43 2 +++ trunk/Estraier.pm 2006/01/04 19:28:30 12 @@ -43,6 +43,8 @@ package Search::Estraier::Document; +use Carp qw/croak confess/; + =head1 Search::Estraier::Document Document for HyperEstraier @@ -58,13 +60,22 @@ my $self = {@_}; bless($self, $class); + $self->{id} = -1; + $self ? return $self : return undef; } + =head2 add_attr +Add an attribute. + $doc->add_attr( name => 'value' ); +Delete attribute using + + $doc->add_attr( name => undef ); + =cut sub add_attr { @@ -72,10 +83,167 @@ my $attrs = {@_}; while (my ($name, $value) = each %{ $attrs }) { - $name =~ s/\s\s+/ /gs; - $value =~ s/\s\s+/ /gs; - push @{$self->{$name}}, $value; + if (! defined($value)) { + delete( $self->{attrs}->{_s($name)} ); + } else { + $self->{attrs}->{_s($name)} = _s($value); + } + } + + return 1; +} + + +=head2 add_text + +Add a sentence of text. + + $doc->add_text('this is example text to display'); + +=cut + +sub add_text { + my $self = shift; + my $text = shift; + return unless defined($text); + + push @{ $self->{dtexts} }, _s($text); +} + + +=head2 add_hidden_text + +Add a hidden sentence. + + $doc->add_hidden_text('this is example text just for search'); + +=cut + +sub add_hidden_text { + my $self = shift; + my $text = shift; + return unless defined($text); + + push @{ $self->{htexts} }, _s($text); +} + +=head2 id + +Get the ID number of document. If the object has never been registred, C<-1> is returned. + + print $doc->id; + +=cut + +sub id { + my $self = shift; + return $self->{id}; +} + +=head2 attr_names + +Returns array with attribute names from document object. + + my @attrs = $doc->attr_names; + +=cut + +sub attr_names { + my $self = shift; + croak "attr_names return array, not scalar" if (! wantarray); + return sort keys %{ $self->{attrs} }; +} + + +=head2 attr + +Returns value of an attribute. + + my $value = $doc->attr( 'attribute' ); + +=cut + +sub attr { + my $self = shift; + my $name = shift; + + return $self->{'attrs'}->{ $name }; +} + + +=head2 texts + +Returns array with text sentences. + + my @texts = $doc->texts; + +=cut + +sub texts { + my $self = shift; + confess "texts return array, not scalar" if (! wantarray); + return @{ $self->{dtexts} }; +} + +=head2 cat_texts + +Return whole text as single scalar. + + my $text = $doc->cat_texts; + +=cut + +sub cat_texts { + my $self = shift; + return join(' ',@{ $self->{dtexts} }); +} + +=head2 dump_draft + + print $doc->dump_draft; + +=cut + +sub dump_draft { + return 'FIXME'; +} + +=head2 delete + +Empty document object + + $doc->delete; + +=cut + +sub delete { + my $self = shift; + + foreach my $data (qw/attrs dtexts stexts/) { + delete($self->{$data}); } + + $self->{id} = -1; + + return 1; +} + + +=head2 _s + +Remove multiple whitespaces from string, as well as whitespaces at beginning or end + + my $text = _s(" this is a text "); + $text = 'this is a text'; + +=cut + +sub _s { + my $text = shift || return; + $text =~ s/\s\s+/ /gs; + $text =~ s/^\s+//; + $text =~ s/\s+$//; + return $text; }