SWISH::API::Object - return SWISH::API results as objects
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); }
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.
SWISH::API::More
SWISH::API::Object is a subclass of SWISH::API::More. Only new or overridden methods are documented here.
Same as in SWISH::API::More.
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).
SWISH::API::Object::Result::Instance
The class should expect at least one property called swish_result which contains the original SWISH::API::Result object.
swish_result
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.
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.
What format should serialized Perl values be assumed to be? The default is yaml. You might also specify json. If you have serialized values in some other format, then you'll need to subclass SWISH::API::Object::Result and override deserialize().
yaml
json
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.
1
See SWISH::Prog::Object.
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.
Get/set the class name passed in new().
Get/set the properties hash ref passed in new().
Utitlity method. Returns sorted array of property names. Shortcut for:
sort keys %{ $swish->properties }
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.
SWISH::API, SWISH::API::More
Peter Karman, <karman@cpan.org>
Thanks to Atomic Learning for supporting some of the development of this module.
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.
To install SWISH::API::Object, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SWISH::API::Object
CPAN shell
perl -MCPAN -e shell install SWISH::API::Object
For more information on module installation, please visit the detailed CPAN module installation guide.