Skip to content

Commit

Permalink
added hasOne association
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Burch committed Feb 1, 2012
1 parent d9e6229 commit 4958f84
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 3 deletions.
31 changes: 31 additions & 0 deletions Base.js
Expand Up @@ -9,6 +9,7 @@ module.exports = function(db, name, config) {
var that = {};

configureHasMany();
configureHasOne();


that.serialize = function() {
Expand Down Expand Up @@ -50,6 +51,36 @@ module.exports = function(db, name, config) {
}
return that;

function configureHasOne() {
Object.keys(config.hasOne || {}).forEach(function(propName) {
var model = domain[config.hasOne[propName]];
var upperPropName = helpers.toUpper(propName);
var idProp = propName + 'Id';
addSetter();
addGetter();


function addSetter() {
that['set' + upperPropName] = function(it) {
if(it && (it.id === undefined)) {
throw 'Can not set non-persisted entity to hasOne';
}
that[idProp] = it && it.id;
}
}

function addGetter() {
that['get' + upperPropName] = function(cb) {
if(that[idProp] !== undefined) {
model.findById(that[idProp], cb);
} else {
cb(undefined);
}
}
}
});
}

function configureHasMany() {

Object.keys(config.hasMany || {}).forEach(function(propName){
Expand Down
2 changes: 1 addition & 1 deletion test/spec/TestUserSpec.js
Expand Up @@ -29,7 +29,7 @@ describe('couch-ar', function () {
},
function () {
// delay so that everything can be setup
setTimeout(asyncSpecDone, 100);
setTimeout(asyncSpecDone, 500);
}
);
asyncSpecWait();
Expand Down
2 changes: 1 addition & 1 deletion test/spec/hasManySpec.js
@@ -1,4 +1,4 @@
var domain = require('couch-ar');
var domain;

describe('hasMany', function() {

Expand Down
83 changes: 83 additions & 0 deletions test/spec/hasOneSpec.js
@@ -0,0 +1,83 @@
var ar = require('couch-ar');

describe('hasMany', function() {

var user, phoneNumber;

beforeEach(function() {
ar = require('couch-ar');

if(ar.TestUser === undefined) {
ar.init({
dbName:'couch-ar-test',
root:__dirname + '/../' + 'testDomain'
}
);
waitsFor(function() {
return ar.TestUser;
});
}

runs(function() {
user = ar.TestUser.create({});
phoneNumber = ar.PhoneNumber.create({id:'pn1'});
});
});

describe('hasOne setter', function() {
it('should set the id of the new entity to the id property', function() {
user.setHomePhoneNumber(phoneNumber);
expect(user.homePhoneNumberId).toBe('pn1');
});

it('will throw exception if you are setting something that is not persisted', function() {
phoneNumber.id = undefined;
expect(function() {user.setHomePhoneNumber(phoneNumber)}).toThrow('Can not set non-persisted entity to hasOne');
});

it('allows you to set undefined', function() {
user.setHomePhoneNumber(undefined);
var callback = jasmine.createSpy();
user.getHomePhoneNumber(callback);
expect(callback).toHaveBeenCalledWith(undefined);

});
});

describe('hasOne getter', function() {

beforeEach(function() {
var cb = jasmine.createSpy();
phoneNumber.save(cb);
waitsFor(function() {
return cb.callCount;
});
});

it('will get entity that is set', function() {
var callback = jasmine.createSpy();
runs(function() {
user.setHomePhoneNumber(phoneNumber);
user.getHomePhoneNumber(callback);
});
waitsFor(function() {
return callback.callCount;
});
runs(function() {
var phoneNumber = callback.argsForCall[0][0];
expect(phoneNumber.id).toBe('pn1');
});
});

it('returns undefined if entity is not set', function() {
var callback = jasmine.createSpy();
expect(user.getHomePhoneNumber(callback));
waitsFor(function() {
return callback.callCount;
});
runs(function() {
expect(callback).toHaveBeenCalledWith(undefined);
});
});
});
});
4 changes: 3 additions & 1 deletion test/testDomain/TestUser.js
Expand Up @@ -13,7 +13,9 @@ domain.create('TestUser',{
phoneNumbers: 'PhoneNumber',
children: {type: 'Child', singular: 'child'}
},

hasOne: {
homePhoneNumber: 'PhoneNumber'
},
views: {
firstOrLastName: {map: function(doc) {
emit(doc.firstName, doc);
Expand Down
3 changes: 3 additions & 0 deletions test/testDomain2/testUser.js
Expand Up @@ -13,6 +13,9 @@ domain.create('TestUser',{
phoneNumbers: 'PhoneNumber',
children: {type: 'Child', singular: 'child'}
},
hasOne: {
homePhoneNumber: 'PhoneNumber'
},
views: {
firstOrLastName: {map: function(doc) {
emit(doc.firstName, doc);
Expand Down

0 comments on commit 4958f84

Please sign in to comment.