Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/factory-lady.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,14 @@
}, function() {
var doc = new model();
var key;
var fn = Factory.setter;
for(key in attrs) {
if(attrs.hasOwnProperty(key)) {
doc[key] = attrs[key];
if(typeof doc[fn] === 'function') {
doc[fn](key, attrs[key]);
} else {
doc[key] = attrs[key];
}
}
}
callback(doc);
Expand All @@ -105,8 +110,9 @@
var assoc = function(name, attr) {
return function(callback) {
create(name, function(doc) {
var fn = Factory.getter;
if(attr) {
callback(doc[attr]);
callback(doc[fn] ? doc[fn](attr) : doc[attr]);
} else {
callback(doc);
}
Expand All @@ -119,6 +125,8 @@
Factory.build = build;
Factory.create = create;
Factory.assoc = assoc;
Factory.setter = null;
Factory.getter = null;

if(typeof module !== 'undefined' && module.exports) {
module.exports = Factory;
Expand Down
51 changes: 50 additions & 1 deletion test/factory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var should = require('should');
var context = describe;

describe('Factory', function() {
var Model, Person, Job;
var Model, Person, Job, Robot;

before(function() {
Model = function() {};
Expand All @@ -18,6 +18,17 @@ describe('Factory', function() {

Job = function() {};
Job.prototype = new Model();

Robot = function() {};
Robot.prototype = new Model();
Robot.prototype.set = function(attr, val) {
this.setterCalled = true;
this[attr] = val;
};
Robot.prototype.get = function(attr) {
this.getterCalled = true;
return this[attr];
};
});

beforeEach(function() {
Expand All @@ -34,6 +45,11 @@ describe('Factory', function() {
title: 'Engineer'
, company: 'Foobar Inc.'
});

Factory.define('robot', Robot, {
name: 'Bender Bending Rodríguez'
, title: Factory.assoc('job', 'title')
});
});

describe('#build', function() {
Expand Down Expand Up @@ -74,6 +90,39 @@ describe('Factory', function() {
});
});
});

context('factory model with getters and setters', function() {
before(function() {
Factory.setter = 'set';
Factory.getter = 'get';
});

after(function() {
Factory.getter = Factory.setter = null;
});

it('builds using defined setter', function(done) {
Factory.build('robot', function(robot) {
(robot instanceof Robot).should.be.true;
robot.get('name').should.eql('Bender Bending Rodríguez');
robot.get('title').should.eql('Engineer');
robot.getterCalled.should.be.true;
robot.setterCalled.should.be.true;
done();
});
});

it('builds assoc. attribute with defined getter', function(done) {
Job.prototype.get = function() {
return 'calledGetter';
};
Factory.build('robot', function(robot) {
robot.title.should.eql('calledGetter')
Job.prototype.get = undefined;
done();
});
});
});
});
});

Expand Down