Skip to content

Commit

Permalink
Merge pull request #86 from BorisKozo/minor-fix-to-registry
Browse files Browse the repository at this point in the history
Minor fix to registry
  • Loading branch information
rhubarbselleven committed Feb 28, 2015
2 parents 3c6c7a5 + b1ae326 commit 0994494
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
4 changes: 2 additions & 2 deletions spec/javascripts/inputReaders.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('input readers', function() {
});

afterEach(function() {
Backbone.Syphon.InputReaders.register('text');
Backbone.Syphon.InputReaders.unregister('text');
});

it('should retrieve the registered "text" reader', function() {
Expand All @@ -39,7 +39,7 @@ describe('input readers', function() {
});

afterEach(function() {
Backbone.Syphon.InputReaders.register('textarea');
Backbone.Syphon.InputReaders.unregister('textarea');
});

it('should be able to retrieve the input reader for that type', function() {
Expand Down
61 changes: 61 additions & 0 deletions spec/javascripts/typeregistry.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
describe('Type Registry', function () {
var typeRegistry;
beforeEach(function () {
typeRegistry = new Backbone.Syphon.TypeRegistry();
});

it('should register a default item', function () {
var item = {};
typeRegistry.registerDefault(item);
expect(typeRegistry.get('default')).to.be.eql(item);
expect(typeRegistry.get()).to.be.eql(item);
expect(typeRegistry.get('some type that doesnt exist')).to.be.eql(item);
});

it('should get an item', function () {
var item = {};
typeRegistry.register('foo', item);
expect(typeRegistry.get('foo')).to.be.eql(item);
});

it('should not get a non existing item', function () {
var item = {};
typeRegistry.register('foo', item);
expect(typeRegistry.get('moo')).to.be.undefined;
});

it('should register an item', function () {
var item = 10;
typeRegistry.register('foo', item);
expect(typeRegistry.get('foo')).to.be.equal(item);
});

it('should register a falsy item', function () {
var item = null;
typeRegistry.register('foo', item);
expect(typeRegistry.get('foo')).to.be.equal(item);
});

it('should re-register and item', function () {
var item1 = {};
var item2 = {};
typeRegistry.register('foo', item1);
typeRegistry.register('foo', item2);
expect(typeRegistry.get('foo')).to.be.eql(item2);
});

it('should unregister a type', function () {
var item = {};
typeRegistry.register('foo', item);
typeRegistry.unregister('foo');
expect(typeRegistry.get('foo')).to.be.undefined;
});

it('should unregister a falsy value', function () {
typeRegistry.register('foo', null);
typeRegistry.unregister('foo');
expect(typeRegistry.get('foo')).to.be.undefined;
});


});
18 changes: 11 additions & 7 deletions src/backbone.syphon.typeregistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Type Registries allow you to register something to
// an input type, and retrieve either the item registered
// for a specific type or the default registration
var TypeRegistry = Syphon.TypeRegistry = function() {
var TypeRegistry = Syphon.TypeRegistry = function () {
this.registeredTypes = {};
};

Expand All @@ -16,24 +16,28 @@ _.extend(TypeRegistry.prototype, {
// Get the registered item by type. If nothing is
// found for the specified type, the default is
// returned.
get: function(type){
return this.registeredTypes[type] || this.registeredTypes['default'];
get: function (type) {
if (_.has(this.registeredTypes, type)) {
return this.registeredTypes[type];
} else {
return this.registeredTypes['default'];
}
},

// Register a new item for a specified type
register: function(type, item) {
register: function (type, item) {
this.registeredTypes[type] = item;
},

// Register a default item to be used when no
// item for a specified type is found
registerDefault: function(item) {
registerDefault: function (item) {
this.registeredTypes['default'] = item;
},

// Remove an item from a given type registration
unregister: function(type) {
if (this.registeredTypes[type]) {
unregister: function (type) {
if (_.has(this.registeredTypes, type)) {
delete this.registeredTypes[type];
}
}
Expand Down

0 comments on commit 0994494

Please sign in to comment.