NAME

DBIx::NoSQL::Store::Manager::StoreModel - trait for attributes linking to store objects.

VERSION

version 1.0.0

SYNOPSIS

package Blog::Model::Entry;

has author => (
    traits => [ 'StoreModel' ],
    store_model =>  'Blog::Model::Author',
    cascade_save => 1,
    cascade_delete => 0,
    is => 'rw',
);

DESCRIPTION

DBIx::NoSQL::Store::Manager::StoreModel (also aliased to StoreModel)

This trait ties the value of the attribute to a model of the store.

The value of the attribute can be set via either a model object, a hashref, or the store key of an object already existing in the store. The getter always returns the inflated model object.

my $blog_entry = $store->create( 'Entry', 
    author => 'yanick',
);

# or
$blog_entry = $store->create( 'Entry', 
    author => Blog::Model::Author->new( name => 'yanick' )
);

# or
$blog_entry = $store->create( 'Entry', 
    author => { name => 'yanick' }
);

my $author_object = $blog_entry->author; # will be a Blog::Model::Author object

If the Array trait is also applied to the attribute, the attribute is assumed to contain a collection of objects. The same logic applies as above, only wrapped in an arrayref.

ATTRIBUTES

store_model => $model_class

Required. Takes in the model associated with the target attribute. Will automatically populate the isa attribute to $model_class|Str_HashRef.

cascade_model => $boolean

Sets the default of cascade_save and cascade_delete. Defaults to false.

cascade_save => $boolean

If true the object associated with the attribute is automatically saved to the store when the main object is save()d.

cascade_delete => $boolean

If true, deletes the attribute object (if there is any) from the store when the main object is delete()d.

If both cascade_delete and cascade_save are true, then when saving the main object, if the attribute object has been modified, its previous value will be deleted from the store.

# assuming the author attribute has `cascade_model => 1`...

my $blog_entry = $store->create( 'Entry', 
    author => Blog::Model::Author->new( 
        name => 'yanick',
        bio  => 'necrohacker',
    ),
);

# store now has yanick as an author

my $pseudonym = $store->create( Author => 
    name => 'yenzie', bio => 'neo-necrohacker' 
);

# store has both 'yanick' and 'yenzie'

# does not modify the store
$blog_entry->author( $pseudonym );

# removes 'yanick'
$blog_entry->save;

AUTHOR

Yanick Champoux <yanick@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2018, 2013, 2012 by Yanick Champoux.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.