Skip to content

Commit

Permalink
Updated Model, Instance and Database constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Dec 13, 2013
1 parent cb018d0 commit ac63605
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ function Database(config) {

"use strict";

if(!(this instanceof Database)) return new Database(config);

this.connection = null;
this.models = {};
this.plugins = [];
Expand Down
5 changes: 4 additions & 1 deletion lib/Instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ function Instance(model, doc, isNew) {
/// </signature>

"use strict";

if(!(this instanceof Instance)) return new Instance(model, doc, isNew);

var $ = this;

if(!isNew) model.fromSource(doc);
Expand Down Expand Up @@ -130,7 +133,7 @@ function Instance(model, doc, isNew) {
if (err) return callback(err);

inserted = inserted[0];
transform.down(options.transforms, inserted);
model.fromSource(inserted);

oldDoc = newDoc = inserted;

Expand Down
8 changes: 5 additions & 3 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function Model(database, collection, schema, options) {
/// <param name="schema" type="Object">A JSON representation of the database schema</param>
/// <param name="options" type="Object">Additional options configuring the behaviour of this model's instances</param>

if(!(this instanceof Model)) return new Model(database, collection, schema, options);

if (!options) options = {};

_.defaults(options, {
Expand All @@ -37,7 +39,7 @@ function Model(database, collection, schema, options) {
]
});

Object.defineProperty(this, 'preprocessor', {
Object.defineProperty(this, 'preprocessors', {
value: new Concoction(options.preprocessors)
});

Expand Down Expand Up @@ -88,14 +90,14 @@ Model.prototype.fromSource = function(document) {
/// <summary>Applies the model's preprocessors to convert the document from the source</summary>
/// <param name="document" type="Object">The object to apply the preprocessors to</param>

this.preprocessor.apply(document);
this.preprocessors.apply(document);
};

Model.prototype.toSource = function(document) {
/// <summary>Applies the model's preprocessors to convert the document from the source</summary>
/// <param name="document" type="Object">The object to apply the preprocessors to</param>

this.preprocessor.reverse(document);
this.preprocessors.reverse(document);
};

Model.prototype.wrap = function (document, isNew) {
Expand Down

0 comments on commit ac63605

Please sign in to comment.