Skip to content

Commit

Permalink
Fix bug with keyspaces not being isolated properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Roberts committed Nov 23, 2013
1 parent 17efab9 commit 8082bac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
20 changes: 14 additions & 6 deletions key.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var util = require('util');

function Key(root, cb) {
this.root = root;
if (cb) cb(this);
Expand All @@ -13,8 +15,8 @@ Key.prototype.valueOf = function () { return this.root; };
Key.prototype.toString = function () { return this.root; };

Key.prototype.prop = function (name) {
Key.prototype[name] = function () {
return new Key(this.root + Key._options.propertySep + name);
this.constructor.prototype[name] = function () {
return new this.constructor(this.root + Key._options.propertySep + name);
};
};

Expand All @@ -30,12 +32,18 @@ Key.prototype.collection = function (plural, singular, cbs) {

singular = singular || plural.substr(0, plural.length - 1);

Key.prototype[plural] = function () {
return new Key(this.root + Key._options.collectionSep + plural, cbs && cbs.collection);
var SubKey = function(root, cb) {
this.root = root;
if (cb) cb(this);
};
util.inherits(SubKey, this.constructor);

this.constructor.prototype[plural] = function () {
return new SubKey(this.root + Key._options.collectionSep + plural, cbs && cbs.collection);
};

Key.prototype[singular] = function (id) {
return new Key(this.root + Key._options.collectionSep + singular + Key._options.memberSep + id, cbs && cbs.member);
this.constructor.prototype[singular] = function (id) {
return new SubKey(this.root + Key._options.collectionSep + singular + Key._options.memberSep + id, cbs && cbs.member);
};
};

Expand Down
21 changes: 21 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ var factory = new Key('myapp', function (f) {
f.prop('members');
}
});

f.collection('foos', {
member: function (f) {
f.prop('bar');
}
});

f.collection('bars', {
member: function (f) {
f.prop('foo');
}
});


});

assert.equal( factory.version(), 'myapp.version' );
Expand All @@ -22,5 +36,12 @@ assert.equal( factory.team('Foo').taskSeq(), 'myapp:team#Foo.taskSeq' );
assert.equal( factory.team('Foo').members(), 'myapp:team#Foo.members' );
assert.equal( factory.team('Foo').tasks(), 'myapp:team#Foo.tasks' );

assert.throws( function() { factory.taskSeq(); }, Error, 'Should die');


assert.equal( factory.foo('FooThing').bar(), 'myapp:foo#FooThing.bar' );
assert.equal( factory.bar('BarThing').foo(), 'myapp:bar#BarThing.foo' );



console.log('All tests passed!');

0 comments on commit 8082bac

Please sign in to comment.