The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Clustericious::Client::Object - default object returned from client methods

SYNOPSIS

my $obj = Clustericious::Client::Object->new({some => 'stuff'});

$obj->some;          # 'stuff'
$obj->some('foo');   # Set to 'foo'

#----------------------------------------------------------------------

package Foo::Object;

use base 'Clustericious::Client::Object';

sub meth { ... };

#----------------------------------------------------------------------

package Foo::OtherObject;

use base 'Clustericious::Client::Object';

our %classes =
(
    myobj => 'Foo::Object'
);

#----------------------------------------------------------------------

my $obj = Foo::Client::OtherObj({ myobj => { my => 'foo' },
                                  some  => 'stuff' });

$obj->myobj->meth();
$obj->myobj->my;       # 'foo'
$obj->some;            # 'stuff'

DESCRIPTION

The Clustericious::Client derived methods receive a possibly nested/complex data structure with their results. This Object helps turn those data structures into simple (or possibly more complex) objects.

By default, it just makes a method for each attribute in the returned data structure. It does this lazily through AUTOLOAD, so it won't make them unless you are using them. If used as a base class, you can override new() to do more initialization (possibly using the client to download more information), or add other methods to the object as needed.

A %classes hash can also be included in a derived class specifying classes to use for certain attributes.

Each Clustericious::Client::Object derived object can also call $obj->_client to get the original client if it was stored with new() (Clustericious::Client does this). This can be used by derived object methods to further interact with the REST server.

METHODS

new

my $obj = Clustericious::Client::Object->new({ some => 'stuff'});

my $obj = Clustericious::Client::Object->new([ { some => 'stuff' } ]);

Makes a hash into an object (or an array of hashes into an array of objects).

You can access or update elements of the hash using method calls: my $x = $obj->some; $obj->some('foo');

In the array case, you can do my $x = $obj->[0]->some;

If a derived class has a %classes package variable, new() will automatically call the right new() for each specified attribute. (See synopsis and examples).

You can also include an optional 'client' parameter:

my $obj = Clustericious::Client::Object->new({ ...}, $client);

that can be retrieved with $obj->_client(). This is useful for derived objects methods which need to access the Clustericious server.

_client

my $obj->_client->do_something();

Access the stashed client. This is useful within derived class methods that need to interact with the server.

SEE ALSO

These are also interesting:

Data::AsObject
Data::Autowrap
Hash::AsObject
Class::Builtin::Hash
Hash::AutoHash
Hash::Inflator
Data::OpenStruct::Deep
Object::AutoAccessor

Mojo::Base
Clustericious::Config