--- trunk/Estraier.pm 2006/01/04 13:33:07 4 +++ trunk/Estraier.pm 2006/01/04 15:04:58 8 @@ -43,6 +43,8 @@ package Search::Estraier::Document; +use Carp qw/confess/; + =head1 Search::Estraier::Document Document for HyperEstraier @@ -58,14 +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' ); +B: delete attribute using + + $doc->add_attr( name => undef ); + =cut sub add_attr { @@ -73,8 +83,96 @@ my $attrs = {@_}; while (my ($name, $value) = each %{ $attrs }) { - push @{$self->{attrs}->{_s($name)}}, _s($value); + $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 + +Get a list of attribute names of a document object. + + my @attrs = $doc->attr_names; + +=cut + +sub attr_names { + my $self = shift; + confess "attr_names return array, not scalar" if (! wantarray); + return sort keys %{ $self->{attrs} }; +} + + +=head2 attr + +Get the value of an attribute. + + my $value = $doc->attr( 'attribute' ); + +=cut + +sub attr { + my $self = shift; + my $name = shift; + + return $self->{'attrs'}->{ $name }; +} + +=head2 dump_draft + + print $doc->dump_draft; + +=cut + +sub dump_draft { } =head2 delete @@ -88,7 +186,9 @@ sub delete { my $self = shift; - delete($self->{attrs}); + foreach my $data (qw/attrs dtexts stexts/) { + delete($self->{$data}); + } return 1; }