diff --git a/lib/repository.js b/lib/repository.js new file mode 100644 index 0000000..340eba3 --- /dev/null +++ b/lib/repository.js @@ -0,0 +1,50 @@ +/* + * Repository + * + * The Repository contains references to all Drivers and Adaptors Cylon is aware + * of, along with which module they live in (e.g. cylon-firmata). + * + * cylonjs.com + * + * Copyright (c) 2013-2014 The Hybrid Group + * Licensed under the Apache 2.0 license. +*/ + +"use strict"; + +var Repository = {}; + +var search = function(entry, value) { + for (var name in Repository) { + var repo = Repository[name]; + + if (~repo[entry].indexOf(value)) { + return repo.module; + } + } + + return false; +}; + +module.exports = { + register: function(module) { + var pkg = require(module); + + Repository[module] = { + module: pkg, + adaptors: pkg.adaptors, + drivers: pkg.drivers + }; + }, + + findByAdaptor: function(adaptor) { + return search("adaptors", adaptor); + }, + + findByDriver: function(driver) { + return search("drivers", driver); + }, + + // here for tests, should not be publicly used + _data: Repository +}; diff --git a/spec/lib/repository.spec.js b/spec/lib/repository.spec.js new file mode 100644 index 0000000..7c9a049 --- /dev/null +++ b/spec/lib/repository.spec.js @@ -0,0 +1,45 @@ +"use strict"; + +var Repository = source('repository'); + +var path = './../spec/support/mock_module.js'; + +var module = require('./../support/mock_module.js') + +describe("Repository", function() { + describe("#register", function() { + it("adds the supplied module to the Registry", function() { + expect(Repository._data).to.be.eql({}); + + Repository.register(path); + + expect(Repository._data).to.be.eql({ + "./../spec/support/mock_module.js": { + module: module, + drivers: ['test-driver'], + adaptors: ['test-adaptor'] + } + }); + }); + }); + + describe("#findByAdaptor", function() { + beforeEach(function() { + Repository.register(path) + }); + + it("finds the appropriate module containing the adaptor", function() { + expect(Repository.findByAdaptor('test-adaptor')).to.be.eql(module); + }); + }); + + describe("#findByDriver", function() { + beforeEach(function() { + Repository.register(path) + }); + + it("finds the appropriate module containing the driver", function() { + expect(Repository.findByDriver('test-driver')).to.be.eql(module); + }); + }); +}); diff --git a/spec/support/mock_module.js b/spec/support/mock_module.js new file mode 100644 index 0000000..7c1f82c --- /dev/null +++ b/spec/support/mock_module.js @@ -0,0 +1,7 @@ +// A mock Cylon module for use in internal testing. +"use strict"; + +module.exports = { + adaptors: [ 'test-adaptor' ], + drivers: [ 'test-driver' ] +};