[KinoSearch] Subclassable Highlighter

Father Chrysostomos sprout at cpan.org
Wed Jan 30 16:17:01 PST 2008




On Jan 30, 2008, at 3:48 PM, Marvin Humphrey wrote:

> A thought about Highlighter's constructor...
>
>  my $highlighter = KinoSearch::Highlight::Highlighter->new(
>    searcher       => $searcher,   # required
>    query          => $query,      # required
>    field          => 'content',   # required
>    excerpt_length => 150,         # default: 200
>    formatter      => $formatter,  # default: a SimpleHTMLFormatter
>    encoder        => $encoder,    # default: a SimpleHTMLEncoder
>  );
>
> In the design we've arrived at, there are a few more constructor  
> args to Highlighter than I might like.  This works OK with labeled  
> args in Perl, but starts to get a little crazy in C or Java:
>
> Highlighter*
> Highlighter_new(const char *class_name, Searcher *searcher, Query  
> *query,
>                ByteBuf *field, u32_t excerpt_length, Formatter  
> *formatter,
>                Encoder *encoder)

Does C have such a thing as optional arguments?

> Plus, the Formatter and the Encoder are esoteric, and I think it  
> makes sense to de-emphasize them.
>
> How about we remove them from the constructor and use defaulting  
> accessors instead?
>
>  sub set_encoder {
>      my ($self, $encoder) = @_;
>      confess("Not a KinoSearch::Highlight::Encoder")
>          unless a_isa_b( $encoder, 'KinoSearch::Highlight::Encoder' );
>      $encoder{$$self} = $encoder;
>  }
>
>  sub get_encoder {
>      my $self = shift;
>      if ( !$encoder{$$self} ) {
>          $encoder{$$self} = KinoSearch::Highlight::SimpleHTMLEncoder- 
> >new;
>      }
>      return $encoder{$$self};
>  }

This sounds fine to me, but I think you’re forgetting about ||= :

sub get_encoder {
     $encoder{${+shift}} ||= KinoSearch::Highlight::SimpleHTMLEncoder- 
 >new
}

Having to pass the query and searcher every time can get a bit  
tedious. How about adding a create_excerpt method to KinoSearch::Simple?


_______________________________________________
KinoSearch mailing list
KinoSearch at rectangular.com
http://www.rectangular.com/mailman/listinfo/kinosearch




More information about the kinosearch mailing list