Permalink
Browse files

Merge pull request #11 from maspwr/master

Add an .after method to add callbacks to Factory builds
  • Loading branch information...
2 parents b45b52c + ddffb2f commit a6dd01247c3882b777147f90bb665cadd091a017 @bkeepers bkeepers committed Feb 4, 2013
Showing with 40 additions and 8 deletions.
  1. +8 −2 README.md
  2. +16 −4 spec/javascripts/rosie.spec.js
  3. +16 −2 src/rosie.js
View
@@ -39,14 +39,20 @@ Which returns an object that looks roughly like:
random_seed: 0.8999513240996748,
players: [
{id: 1, name:'Player 1'},
- {id: 1, name:'Player 2'}
+ {id: 2, name:'Player 2'}
]
}
For a factory with a constructor, if you want just the attributes:
Factory.attributes('game') // return just the attributes
+
+You can also define a callback function to be run after building an object:
+
+ Factory.define('coach').after(function(coach, options) { if (options.buildPlayer) { Factory.build('player', {coach_id: coach.id}; } })
+
+ Factory.build('coach', {}, {buildPlayer: true});
## Credits
-Thanks to [Daniel Morrison](http://twitter.com/danielmorrison/status/58883772040486912) for the name and [Jon Hoyt](http://twitter.com/jonmagic) for inspiration and brainstorming the idea.
+Thanks to [Daniel Morrison](http://twitter.com/danielmorrison/status/58883772040486912) for the name and [Jon Hoyt](http://twitter.com/jonmagic) for inspiration and brainstorming the idea.
@@ -12,7 +12,9 @@ describe('Factory', function() {
};
beforeEach(function() {
- Factory.define('thing', Thing).attr('name', 'Thing 1');
+ Factory.define('thing', Thing).attr('name', 'Thing 1').after(function(obj) {
+ obj.afterCalled = true;
+ });
});
it('should return a new instance of that constructor', function() {
@@ -21,7 +23,11 @@ describe('Factory', function() {
});
it('should set attributes', function() {
- expect(Factory.build('thing')).toEqual({name: 'Thing 1'});
+ expect(Factory.build('thing')).toEqual({name: 'Thing 1', afterCalled: true});
+ });
+
+ it('should run callbacks', function() {
+ expect(Factory.build('thing').afterCalled).toBe(true);
});
});
@@ -42,12 +48,18 @@ describe('Factory', function() {
describe('extend', function() {
beforeEach(function() {
- Factory.define('thing').attr('name', 'Thing 1');
+ Factory.define('thing').attr('name', 'Thing 1').after(function(obj) {
+ obj.afterCalled = true;
+ });
Factory.define('anotherThing').extend('thing').attr('title', 'Title 1');
});
it('should extend attributes', function() {
- expect(Factory.build('anotherThing')).toEqual({name:'Thing 1', title:'Title 1'});
+ expect(Factory.build('anotherThing')).toEqual({name:'Thing 1', title:'Title 1', afterCalled: true});
+ });
+
+ it('should extend callbacks', function() {
+ expect(Factory.build('anotherThing').afterCalled).toBe(true);
});
});
View
@@ -2,6 +2,7 @@ var Factory = function(constructor) {
this.construct = constructor;
this.attrs = {};
this.sequences = {};
+ this.callbacks = [];
};
Factory.prototype = {
@@ -21,6 +22,11 @@ Factory.prototype = {
return this;
},
+ after: function(callback) {
+ this.callbacks.push(callback);
+ return this;
+ },
+
attributes: function(attrs) {
attrs = attrs || {};
for(var attr in this.attrs) {
@@ -43,6 +49,10 @@ Factory.prototype = {
this.attrs[attr] = factory.attrs[attr];
}
}
+ // Copy the parent's callbacks
+ for(var i = 0; i < factory.callbacks.length; i++) {
+ this.callbacks.push(factory.callbacks[i]);
+ }
return this;
}
};
@@ -55,8 +65,12 @@ Factory.define = function(name, constructor) {
return factory;
};
-Factory.build = function(name, attrs) {
- return this.factories[name].build(attrs);
+Factory.build = function(name, attrs, options) {
+ var obj = this.factories[name].build(attrs);
+ for(var i = 0; i < this.factories[name].callbacks.length; i++) {
+ this.factories[name].callbacks[i](obj, options);
+ }
+ return obj;
};
Factory.attributes = function(name, attrs) {

0 comments on commit a6dd012

Please sign in to comment.