Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request rosiejs#11 from maspwr/master
Browse files Browse the repository at this point in the history
Add an .after method to add callbacks to Factory builds
  • Loading branch information
bkeepers committed Feb 4, 2013
2 parents b45b52c + ddffb2f commit a6dd012
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
10 changes: 8 additions & 2 deletions README.md
Expand Up @@ -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.
20 changes: 16 additions & 4 deletions spec/javascripts/rosie.spec.js
Expand Up @@ -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() {
Expand All @@ -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);
});
});

Expand All @@ -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);
});
});

Expand Down
18 changes: 16 additions & 2 deletions src/rosie.js
Expand Up @@ -2,6 +2,7 @@ var Factory = function(constructor) {
this.construct = constructor;
this.attrs = {};
this.sequences = {};
this.callbacks = [];
};

Factory.prototype = {
Expand All @@ -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) {
Expand All @@ -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;
}
};
Expand All @@ -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) {
Expand Down

0 comments on commit a6dd012

Please sign in to comment.