Permalink
Browse files

Moved to key-based Factory definitions instead of classes as keys

  • Loading branch information...
1 parent 576ee89 commit 4fa4f300783e993d7dc5714685829640ffaa4af4 @cadwallion cadwallion committed Jul 8, 2011
Showing with 9 additions and 8 deletions.
  1. +2 −1 README.md
  2. +3 −3 examples/mongoose.js
  3. +4 −4 index.js
View
@@ -19,12 +19,13 @@ To begin, we need to define our Factory. We do this with the Factory.define met
``` js
var Factory = require('factory-worker');
- Factory.define(Profile, {
+ Factory.define("profile", Profile, {
"username":"Coolguy123",
"first_name":"Cool",
"last_name":"Guy"
})
```
+In the example above, we've created the "profile" Factory for the class Profile with our default attributes.
From there, we are able to use the `Factory.build()` and `Factory.create()` methods to create new objects of the time. The difference between build and create is that create will automatically call the save() method on the built object.
View
@@ -12,11 +12,11 @@ var Profile = mongoose.model('Profile', ProfileSchema);
var Factory = require('factory-worker');
-Factory.define(Profile, {
+Factory.define('profile', Profile, {
"username":"Coolguy123",
"first_name":"Cool",
"last_name":"Guy"
})
-object = Factory.create(Profile)
-object2 = Factory.build(Profile, { "username": "Anotherguy" })
+object = Factory.create('profile')
+object2 = Factory.build('profile', { "username": "Anotherguy" })
View
@@ -6,16 +6,16 @@
function Factory() {}
Factory.patterns = Hash({});
- Factory.define = function(model, def) {
- Factory.patterns[model] = def;
+ Factory.define = function(key, model, def) {
+ Factory.patterns[key] = { "class": model, "attributes": def };
}
Factory.build = function(model, data) {
if (data === undefined) {
data = {};
}
- object_data = Hash.merge(Factory.patterns[model], data);
- object = new(model)(object_data);
+ object_data = Hash.merge(Factory.patterns[model]["attributes"], data);
+ object = new(Factory.patterns[model]["class"])(object_data);
}
Factory.create = function(model, data) {
object = Factory.build(model, data);

0 comments on commit 4fa4f30

Please sign in to comment.