Skip to content

Instantly share code, notes, and snippets.

@dburles
Created November 25, 2013 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dburles/7639410 to your computer and use it in GitHub Desktop.
Save dburles/7639410 to your computer and use it in GitHub Desktop.
Factory = function() {
var factoryName;
var factories = {};
this.define = function(name, collection, attributes) {
factoryName = name;
factories[name] = {
name: name,
collection: collection,
attributes: attributes,
has: []
};
return this;
};
this.has = function(name) {
console.log(factoryName + ' has ' + name);
factories[factoryName].has.push(name);
return this;
};
this.build = function(name) {
console.log('build: ' + name);
var insertId = this._insert(name);
return factories[name].collection.findOne(insertId);
};
this._insert = function(name) {
var self = this;
var factory = factories[name];
var insertId = factory.collection.insert(factory.attributes);
_.each(factory.has, function(n) {
var update = {};
var relId = self._insert(n);
update[n + 'Id'] = relId;
factory.collection.update(insertId, { $set: update });
});
return insertId;
};
this.debug = function() {
console.log('factories: ', factories);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment