Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ Factory 'post', (post) ->
# same as Factory.create
```

## Running tests:

```npm i
node ./node_modules/mocha/bin/mocha ./test
```

## License

Copyright (c) 2011 Peter Jihoon Kim. This software is licensed under the [MIT License](http://github.com/petejkim/factory-lady/raw/master/LICENSE).
Expand Down
35 changes: 22 additions & 13 deletions lib/factory-lady.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@
};
};

var instantiate = function(model, attrs) {
var key, doc = new model();
for(key in attrs) {
if(attrs.hasOwnProperty(key)) {
doc[key] = attrs[key];
}
}
return doc;
}

var build = function(name, userAttrs, callback) {
if(typeof userAttrs === 'function') {
callback = userAttrs;
Expand All @@ -75,12 +85,15 @@
cb();
}
}, function() {
var doc = new model();
var key;
for(key in attrs) {
if(attrs.hasOwnProperty(key)) {
doc[key] = attrs[key];
}
var doc = Factory.instantiate(model, attrs);
callback(doc);
});
};

var save = function(doc, callback) {
doc.save(function(err) {
if(err) {
throw err;
}
callback(doc);
});
Expand All @@ -93,12 +106,7 @@
}

build(name, userAttrs, function(doc) {
doc.save(function(err) {
if(err) {
throw err;
}
callback(doc);
});
Factory.save(doc, callback);
});
};

Expand All @@ -119,11 +127,12 @@
Factory.build = build;
Factory.create = create;
Factory.assoc = assoc;
Factory.save = save;
Factory.instantiate = instantiate;

if(typeof module !== 'undefined' && module.exports) {
module.exports = Factory;
} else {
this.Factory = Factory;
}
}());