The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

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'
                    );
                    
  my $results = $swish->query('foo');
  
  while(my $object = $results->next_result)
  {
    # $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

SWISH::API::More

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).

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.

serial_format

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().

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.

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::Result 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.

deserialize( format, prop_val )

Called for each property value. The format deserialize() expects is based on serial_format in SWISH::API::Object->new().

SEE ALSO

SWISH::API, SWISH::API::More

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.