NAME
SWISH::API::More - do more with the SWISH::API
SYNOPSIS
package My::SwishAPI;
use base qw( SWISH::API::More );
sub init
{
my $self = shift;
$self->SUPER::init(@_);
# wrap every SWISH::API method with one of your own
$self->{wrappers} = {
'My::SwishAPI' => sub {
my $sam = shift;
$sam->do_something(@_);
}
};
$self->make_methods;
$self->wrap_methods;
}
sub do_something
{
my $self = shift; # My::SwishAPI object
}
# or subclass in a traditional way
sub new_search_object
{
my $self = shift;
my $swish_handle = $self->handle;
# do something with $swish_handle
}
1;
# set _before and _after methods
# NOTE the perl and C-style names
package My::SwishAPI::Results;
sub Hits_before
{
my $self = shift;
$self->logger("fetching hit count");
return;
}
sub hits_before { Hits_before(@_) }
sub Hits_after
{
my $self = shift;
my ($origref,$args,$ret) = @_;
my $h = $ret->[0];
$self->logger("found $h hits");
return $h;
}
sub hits_after { Hits_after(@_) }
1;
# else where in a script
use My::SwishAPI;
my $swish = My::SwishAPI->new(
indexes => [qw( path/to/index1 path/to/index2 )],
log => $a_filehandle
);
$swish->logger("opened a new swish-e handle");
# use $swish just like you would with SWISH::API->new object.
# only do More!
DESCRIPTION
SWISH::API::More is a base class for subclassing and extending SWISH::API. Since SWISH::API is just a thin Perl XS wrapper around the Swish-e C library, which isn't friendly for subclassing in a traditional Perlish way, SWISH::API::More allows you to subclass SWISH::API like you would a native Perl module.
SWISH::API::More does some ugly Symbol table mangling to achieve its magic. Don't look at the wizard behind the curtain.
REQUIREMENTS
SWISH::API, Class::Accessor::Fast
METHODS
new( @args )
Creates a new SWISH::API::More object.
args may be either a string of space-separated index names (like SWISH::API uses) or key/value pairs.
Example:
my $swish = SWISH::API::More->new(
indexes => [qw( my/index )],
log => *{STDERR}, # logger will print to stderr
);
You can use the returned $swish
object just like a SWISH::API object. But you can also use the defined methods in SWISH::API::More -- or create your own by subclassing (see SYNOPSIS).
You probably don't want to override new() in a subclass. See init() instead.
init
This makes the magic happen. If you subclass SWISH::API::More you'll likely want to override init(). See SWISH::API::Stat for an example.
init() is called internally by new(). Override init() not new().
handle
Returns the SWISH::API handle. The handle is simply a SWISH::API object. So this:
my $s = SWISH::API->new;
and this:
my $s = SWISH::API::More->new->handle;
give you the same thing. Except SWISH::API::More gives you More.
indexes
Get/set the indexes to which you connect with handle(). indexes() contains an arrayref only. The SWISH::API-style space-separated string feature in new() is stored as an arrayref internally and that's what indexes() returns.
log
Get/set the filehandle that logger() prints to. Defaults to STDERR. Set to 0
to disable the default (but then don't expect logger() to work...).
logger( msg )
Will print msg to the filehandle set in log().
EXAMPLES
See the SWISH::API::Stat module for a working example.
SEE ALSO
AUTHOR
Peter Karman, <karman@cpan.org>
Thanks to Atomic Learning for supporting some of the development of this module.
COPYRIGHT AND LICENSE
Copyright (C) 2006 by Peter Karman
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.