NAME
Mandel::Document - A single MongoDB document with logic
SYNOPSIS
package MyModel::MyType;
use Mandel::Document 'some_collection_name';
package MyModel::MyType;
use Mandel::Document (
collection => 'some_collection_name',
collection_class => 'My::Custom::Collection',
);
DESCRIPTION
Mandel is a simplistic model layer using the Mango module to interact with a MongoDB backend. The Mandel class defines the overall model, including high level interaction. Individual results, called Types inherit from Mandel::Document.
ATTRIBUTES
Mandel inherits all attributes from Mojo::Base and implements the following new ones.
id
$object_id = $self->id;
$self = $self->id("507f1f77bcf86cd799439011");
$self = $self->id(Mango::BSON::ObjectID->new);
Returns the Mango::BSON::ObjectID object for this document. Will create one if it does not already exist.
This can field can also be set.
in_storage
Boolean true if this document has been fetched from storage or saved to storage.
connection
An instance of Mandel. This is required.
model
Returns a Mandel::Model object. This object is a class variable and therefor shared between all instances.
dirty
This attribute holds a hash-ref where the keys are name of fields that has been updated or otherwise not stored in database.
TODO: Define what the values should hold. Timestamp? A counter for how many times the field has been updated before saved..?
METHODS
Mandel::Document inherits all of the methods from Mojo::Base and implements the following new ones.
new
Constructs a new object.
initialize
A no-op placeholder useful for initialization (see "initialize" in Mandel)
get
$any = $self->get('/json/2/pointer');
Use Mojo::JSON::Pointer to retrieve a value inside the raw mongodb document.
is_changed
Returns true if "dirty" contains any field names.
remove
$self = $self->remove(sub { my($self, $err) = @_; });
Will remove this object from the "collection" and set mark all fields as "dirty".
save
$self = $self->save(sub { my($self, $err) = @_; });
This method stores the raw data in the database and collection. It clear the "dirty" attribute.
NOTE: This method will call the callback (with $err set to empty string) immediately unless "is_changed" returns true.
set
$self = $self->set('/json/2/pointer', $val);
Use a JSON pointer to set data in the raw mongodb document. This method will die if the pointer points to non-compatible data.
import
See "SYNOPSIS".
SEE ALSO
AUTHOR
Jan Henning Thorsen - jhthorsen@cpan.org
Joel Berger, <joel.a.berger@gmail.com>