NAME
SWISH::API::Object - return SWISH::API results as objects
SYNOPSIS
use SWISH::API::Object;
my $swish = SWISH::API::Object->new(
indexes => [ qw( my/index/1 my/index/2 )],
class => 'My::Class',
properties => {
swishlastmodified => 'result_property_str',
myproperty => 1,
},
stash => {
dbh => DBI->connect($myinfo)
},
serial_format => 'yaml',
filter => sub { my ($sao, $result) = @_; return 1 },
);
my $results = $swish->query('foo');
while ( my $object = $results->next ) {
# $object is a My::Class object
for my $prop ($swish->props) {
printf("%s = %s\n", $prop, $object->$prop);
}
# $object also has all methods of My::Class
printf("mymethod = %s\n", $object->mymethod);
}
DESCRIPTION
SWISH::API::Object changes your SWISH::API::Result object into an object blessed into the class of your choice.
SWISH::API::Object will automatically create accessor methods for every result property you specify, or all of them if you don't specify any.
In addition, the result object will inherit all the methods and attributes of the class you specify. If your class has a new() method, it will be called for you. Otherwise, a generic new() method will be used.
REQUIREMENTS
METHODS
SWISH::API::Object is a subclass of SWISH::API::More. Only new or overridden methods are documented here.
new
- indexes
-
Same as in SWISH::API::More.
- class
-
The class into which your Result object will be blessed. If not specified, the index header will be searched according to the API specified in SWISH::Prog::Object, and if no suitable class name is found, will default to
SWISH::API::Object::Result::Instance
, which is a subclass of Class::Accessor::Fast (whose magic is inherited from SWISH::API::More).The class should expect at least one property called
swish_result
which contains the original SWISH::API::Result object. - properties
-
A hash ref of PropertyNames and their formats. Keys are PropertyNames you'd like made into accessor methods. Values are the SWISH::API::Property methods you'd like called on each property value when it is set in the object.
The default is to use all PropertyNames defined in the index, with the default format.
- stash
-
Pass along any data you want to the Result object. Examples might include passing a DBI handle so your object could query a database directly based on some method you define. The stash value should be a hash reference, whose keys/values will be merged and supercede the properties values passed to the class new() method.
- serial_format
-
What format should serialized Perl values be assumed to be? The default is
yaml
. You might also specifyjson
. If you have serialized values in some other format, then you'll need to subclass SWISH::API::Object::Result and override deserialize().If your properties are simple strings, numbers or dates, and you haven't indexed them as serialized objects, then just set serial_format equal to
1
.See SWISH::Prog::Object.
- filter
-
Pass in a CODE ref to filter results in the SWISH::API::Object::Results next_result() method. Your filter should expect two arguments: the SWISH::API::Object object and a SWISH::API::More::Result object.
Your filter may use the filter_cache() method on the S::A::O object to stash data between next_result() calls. The default return value of filter_cache() is an empty hash ref.
If your filter returns true, the result will be object-ified and returned. If false, then next_result() will be called again internally and the next SWISH::API::Result object passed on to your filter.
class
Get/set the class name passed in new().
properties
Get/set the properties hash ref passed in new().
props
Utitlity method. Returns sorted array of property names. Shortcut for:
sort keys %{ $swish->properties }
SWISH::API::Object::Result
The internal SWISH::API::Object::Results class is used to extend the SWISH::API next_result() method with a next_result_after() method. See SWISH::API::More for documentation about how the *_after() methods work.
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) 2008 by Peter Karman
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.