Mandel::Document - A single MongoDB document with logic
Extend a class with MyDocument::Class instead of Mandel::Document:
MyDocument::Class
package MyModel::Person; use Mandel::Document "MyDocument::Class";
Specify a default collection name, instead of the default. "import" will think you meant a base class, if this argument contains "::".
package MyModel::Person; use Mandel::Document "some_collection_name";
Spell out the options with a list:
package MyModel::Person; use Mandel::Document ( extends => "My::Document::Class", collection_name => "some_collection_name", collection_class => "My::Custom::Collection", );
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.
An object of this class gets automatically serialized by Mojo::JSON. See "TO_JSON" and Mojo::JSON#DESCRIPTION for details.
Example:
use Mojolicious::Lite; # ... get '/some/resource' => sub { my $c = shift; # find some document... $c->render(json => $mandel_doc_object); };
Mandel inherits all attributes from Mojo::Base and implements the following new ones.
$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.
Holds the raw mongodb document. It is possible to define default values for this attribute by defining a _build_data() method in the sub class. Example:
_build_data()
sub _build_data { my $self = shift; return { age => 0, name => '' }; }
Boolean true if this document has been fetched from storage or saved to storage.
An instance of Mandel. This is required.
Returns a Mandel::Model object. This object is a class variable and therefor shared between all instances.
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..?
Mandel::Document inherits all of the methods from Mojo::Base and implements the following new ones.
Constructs a new object.
A no-op placeholder useful for initialization. See "initialize" in Mandel.
$bool = $self->get('/json/2/pointer');
Use "contains" in Mojo::JSON::Pointer to check if a value exists inside the raw mongodb document.
$any = $self->get('/json/2/pointer');
Use "get" in Mojo::JSON::Pointer to retrieve a value inside the raw mongodb document.
Returns true if "dirty" contains any field names.
$self = $self->patch(\%changes, sub { my($self, $err) = @_ }); $self = $self->patch(\%changes);
This method will insert/update a partial document. This is useful if /data does not contain a complete document.
/data
It will also insert the document if a document with "id" does not already exist.
$self = $self->remove(sub { my($self, $err) = @_; }); $self = $self->remove;
Will remove this object from the "collection" and set mark all fields as "dirty".
$self = $self->save(sub { my($self, $err) = @_; }); $self = $self->save;
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" is true and "in_storage" is false.
$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.
See "SYNOPSIS".
Alias for "data".
This method allow the document to get automatically serialized by Mojo::JSON.
Mojolicious, Mango, Mandel
Jan Henning Thorsen - jhthorsen@cpan.org
jhthorsen@cpan.org
To install Mandel, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mandel
CPAN shell
perl -MCPAN -e shell install Mandel
For more information on module installation, please visit the detailed CPAN module installation guide.