Permalink
Browse files

Adds specs, cleans up factory code, version bumps to 0.3.0, updates R…

…EADME
  • Loading branch information...
1 parent 1bf9691 commit 21911d79f676afaf96474cbaf9500eda6b549352 @cadwallion cadwallion committed Jan 6, 2012
Showing with 132 additions and 37 deletions.
  1. +1 −0 .gitignore
  2. +28 −12 README.md
  3. +28 −23 index.js
  4. +4 −2 package.json
  5. +63 −0 spec/factory_spec.js
  6. +8 −0 spec/spec_helper.js
View
@@ -0,0 +1 @@
+node_modules/*
View
@@ -1,18 +1,15 @@
-Factory Worker
-===
+# Factory Worker
Factory Worker is a node.js library that creates the Factory pattern for object stores. It assumes the objects are building respond to new() and save(). Other than that, it does not care.
-Installation
----
+## Installation
Factory-worker is available on npm:
`npm install factory-worker`
-Usage
----
+## Usage
To begin, we need to define our Factory. We do this with the Factory.define method:
@@ -25,17 +22,36 @@ To begin, we need to define our Factory. We do this with the Factory.define met
"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.
+
+*build(factory, [attributes])*
+
+Builds an instance of the factory, overriding the factory with the attributes hash if passed.
+
+*create(factory, [attributes], callback)*
+
+Calls the `build` function followed by calling `save(callback)` on the created object.
See the `examples` directory for more information.
+## Testing
+
+FactoryWorker uses jessie for tests. If you have jessie globally installed (via `npm install -g jessie`)
+you can run with `jessie spec`. If you are like me and consider the -g switch akin to `sudo gem install`,
+you can run the specs with `./node_modules/.bin/jessie spec` and you're all set.
+
+## Contributing
+
+Contributions are welcome, submit a pull request and I'll happily take a look. I only ask that you don't alter the
+version of the library in your pull request.
+
+
+## Credit
-Credit
----
+This library was inspired by [factory_girl](http://github.com/thoughtbot/factory_girl/) because I wanted something similar while working in NodeJS.
+It is by no means as fully functional as factory\_girl, but it's a start.
-This library was inspired by [factory_girl](http://github.com/thoughtbot/factory_girl/) because I wanted something similar while working in NodeJS. It is by no means as fully functional as factory_girl, but it's a start.
+* [Andrew Nordman](http://github.com/cadwallion/) <anordman@agoragames.com>
-* Author: Andrew Nordman <anordman@agoragames.com>
-* GitHub: http://github.com/cadwallion/
View
@@ -1,26 +1,31 @@
-(function() {
- var Hash = require('hashish');
-
- module.exports = Factory;
-
- function Factory() {}
-
- Factory.patterns = Hash({});
- Factory.define = function(key, model, def) {
- Factory.patterns[key] = { "class": model, "attributes": def };
- }
- Factory.build = function(model, data) {
+var Hash = require('hashish');
+
+module.exports = {
+ patterns: {},
+ define: function(key, model, def) {
+ this.patterns[key] = {
+ class: model,
+ attributes: def
+ }
+ },
+ build: function(model, data) {
if (data === undefined) {
- data = {};
+ data = {}
}
-
- object_data = Hash.merge(Factory.patterns[model]["attributes"], data);
- object = new(Factory.patterns[model]["class"])(object_data);
- return object;
- }
- Factory.create = function(model, data) {
- object = Factory.build(model, data);
- object.save();
- return object;
+
+ object_data = Hash.merge(this.patterns[model].attributes, data)
+ obj = new(this.patterns[model].class)(object_data)
+ return obj
+ },
+ create: function(model, data, callback) {
+ if (typeof(data) == 'function') {
+ callback = data;
+ data = {}
+ }
+
+ var obj = this.build(model, data)
+ obj.save(function(error) {
+ callback(error, obj)
+ })
}
-})();
+}
View
@@ -4,7 +4,7 @@
"description": "NodeJS Datastore-agnostic Factory pattern",
"keywords": ["factory", "datastore", "testing"],
"main": "./index.js",
- "version": "0.2.2",
+ "version": "0.3.0",
"repository": {
"type": "git",
"url": "git://github.com/agoragames/factory-worker.git"
@@ -15,5 +15,7 @@
"dependencies": {
"hashish": ">=0.0.3"
},
- "devDependencies": {}
+ "devDependencies": {
+ "jessie": ">=0.3.7"
+ }
}
View
@@ -0,0 +1,63 @@
+describe('Factory#define', function() {
+ it('stores the definition on the factory name', function() {
+ Factory.define('test', TestModel, {
+ name: 'Test Model',
+ real: false
+ })
+
+ expect(Factory.patterns.test).toEqual({
+ class: TestModel,
+ attributes: {
+ name: 'Test Model',
+ real: false
+ }
+ })
+ })
+})
+
+describe('Factory#build', function() {
+ beforeEach(function() {
+ Factory.define('test', TestModel, {
+ name: 'Test Model',
+ real: false
+ })
+ })
+
+ it('uses the stored definition to seed attribute values', function() {
+ object = Factory.build('test')
+ expect(object.name).toEqual('Test Model')
+ expect(object.real).toEqual(false)
+ })
+
+ it('takes an optional parameter to override stores attributes', function() {
+ object = Factory.build('test', { real: true })
+ expect(object.name).toEqual('Test Model')
+ expect(object.real).toEqual(true)
+ })
+})
+
+describe('Factory#create', function() {
+ beforeEach(function() {
+ Factory.define('test', TestModel, {
+ name: 'Test Model',
+ real: false
+ })
+ })
+
+ it('should call Factory.build, passing the factory name and data', function() {
+ spyOn(Factory, 'build').andCallThrough()
+ Factory.create('test', function(error) {})
+ expect(Factory.build).toHaveBeenCalledWith('test', {})
+ })
+
+ it('can also override attributes like Factory#build', function() {
+ var object;
+ Factory.create('test', { name: 'Test Object' }, function(e, o) {
+ object = o;
+ })
+ waitsFor(function() { return object != null}, 'object creation failed', 1000)
+ runs(function() {
+ expect(object.name).toEqual('Test Object')
+ })
+ })
+})
View
@@ -0,0 +1,8 @@
+Factory = require('../')
+TestModel = function TestModel(attributes) {
+ this.name = attributes.name;
+ this.real = attributes.real;
+}
+TestModel.prototype.save = function(callback) {
+ callback(null)
+}

0 comments on commit 21911d7

Please sign in to comment.