Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 4fa4f300783e993d7dc5714685829640ffaa4af4 1 parent 576ee89
Andrew Nordman authored July 08, 2011
3  README.md
Source Rendered
@@ -19,12 +19,13 @@ To begin, we need to define our Factory.  We do this with the Factory.define met
19 19
 ``` js
20 20
   var Factory = require('factory-worker');
21 21
   
22  
-  Factory.define(Profile, {
  22
+  Factory.define("profile", Profile, {
23 23
     "username":"Coolguy123",
24 24
     "first_name":"Cool",
25 25
     "last_name":"Guy"
26 26
   })
27 27
 ```
  28
+In the example above, we've created the "profile" Factory for the class Profile with our default attributes.
28 29
 
29 30
 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.
30 31
 
6  examples/mongoose.js
@@ -12,11 +12,11 @@ var Profile = mongoose.model('Profile', ProfileSchema);
12 12
 
13 13
 var Factory = require('factory-worker');
14 14
 
15  
-Factory.define(Profile, {
  15
+Factory.define('profile', Profile, {
16 16
   "username":"Coolguy123",
17 17
   "first_name":"Cool",
18 18
   "last_name":"Guy"
19 19
 })
20 20
 
21  
-object = Factory.create(Profile)
22  
-object2 = Factory.build(Profile, { "username": "Anotherguy" })
  21
+object = Factory.create('profile')
  22
+object2 = Factory.build('profile', { "username": "Anotherguy" })
8  index.js
@@ -6,16 +6,16 @@
6 6
   function Factory() {}
7 7
   
8 8
   Factory.patterns = Hash({});
9  
-  Factory.define = function(model, def) {
10  
-    Factory.patterns[model] = def;
  9
+  Factory.define = function(key, model, def) {
  10
+    Factory.patterns[key] = { "class": model, "attributes": def };
11 11
   }
12 12
   Factory.build = function(model, data) {
13 13
     if (data === undefined) {
14 14
       data = {};
15 15
     }
16 16
     
17  
-    object_data = Hash.merge(Factory.patterns[model], data);
18  
-    object = new(model)(object_data);
  17
+    object_data = Hash.merge(Factory.patterns[model]["attributes"], data);
  18
+    object = new(Factory.patterns[model]["class"])(object_data);
19 19
   }
20 20
   Factory.create = function(model, data) {
21 21
     object = Factory.build(model, data);

0 notes on commit 4fa4f30

Please sign in to comment.
Something went wrong with that request. Please try again.