--- trunk/Estraier.pm 2006/01/04 14:38:35 5 +++ trunk/Estraier.pm 2006/01/04 15:28:39 9 @@ -43,6 +43,8 @@ package Search::Estraier::Document; +use Carp qw/croak confess/; + =head1 Search::Estraier::Document Document for HyperEstraier @@ -58,15 +60,19 @@ 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 +Delete attribute using $doc->add_attr( name => undef ); @@ -77,13 +83,21 @@ my $attrs = {@_}; while (my ($name, $value) = each %{ $attrs }) { - push @{ $self->{attrs}->{_s($name)} }, _s($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 @@ -99,6 +113,8 @@ =head2 add_hidden_text +Add a hidden sentence. + $doc->add_hidden_text('this is example text just for search'); =cut @@ -111,6 +127,64 @@ 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 "attr_names return array, not scalar" if (! wantarray); + return $self->{dtexts}; +} + =head2 dump_draft print $doc->dump_draft; @@ -118,6 +192,7 @@ =cut sub dump_draft { + return 'FIXME'; } =head2 delete