Repository module added, not integrated yet

This commit is contained in:
Andrew Stewart 2014-10-29 15:25:35 -07:00
parent 0cd85988e4
commit 63e43cc553
3 changed files with 102 additions and 0 deletions

50
lib/repository.js Normal file
View File

@ -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
};

View File

@ -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);
});
});
});

View File

@ -0,0 +1,7 @@
// A mock Cylon module for use in internal testing.
"use strict";
module.exports = {
adaptors: [ 'test-adaptor' ],
drivers: [ 'test-driver' ]
};