First crack at removing seperate Device/Connection initialization

This commit is contained in:
Andrew Stewart 2015-01-22 14:40:09 -08:00
parent d56f9bc53f
commit 147816b366
8 changed files with 67 additions and 200 deletions

View File

@ -1,69 +0,0 @@
/*
* connection
* cylonjs.com
*
* Copyright (c) 2013-2015 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
"use strict";
var Registry = require("./registry"),
Config = require("./config"),
_ = require("./lodash");
var testMode = function() {
return process.env.NODE_ENV === "test" && Config.testMode;
};
// Public: Creates a new Adaptor and returns it.
//
// opts - hash of acceptable params:
// robot - Robot the Connection belongs to
// name - name for the connection
// adaptor - string module name of the adaptor to be set up
// port - string port to use for the Connection
//
// Returns the newly set-up connection
module.exports = function Connection(opts) {
var module;
opts = opts || {};
if (opts.module) {
module = Registry.register(opts.module);
} else {
module = Registry.findByAdaptor(opts.adaptor);
}
if (!module) {
Registry.register("cylon-" + opts.adaptor);
module = Registry.findByAdaptor(opts.adaptor);
}
var adaptor = module.adaptor(opts);
_.forIn(adaptor, function(prop, name) {
if (name === "constructor") {
return;
}
if (_.isFunction(prop)) {
adaptor[name] = prop.bind(adaptor);
}
});
if (testMode()) {
var testAdaptor = Registry.findByAdaptor("test").adaptor(opts);
_.forIn(adaptor, function(prop, name) {
if (_.isFunction(prop) && !testAdaptor[name]) {
testAdaptor[name] = function() { return true; };
}
});
return testAdaptor;
}
return adaptor;
};

View File

@ -1,70 +0,0 @@
/*
* device
* cylonjs.com
*
* Copyright (c) 2013-2015 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
"use strict";
var Registry = require("./registry"),
Config = require("./config"),
_ = require("./lodash");
var testMode = function() {
return process.env.NODE_ENV === "test" && Config.testMode;
};
// Public: Creates a new Device
//
// opts - object containing Device params
// name - string name of the device
// pin - string pin of the device
// robot - parent Robot to the device
// connection - connection to the device
// driver - string name of the module the device driver logic lives in
//
// Returns a new Device
module.exports = function Device(opts) {
var module;
if (opts.module) {
module = Registry.register(opts.module);
} else {
module = Registry.findByDriver(opts.driver);
}
opts.device = this;
if (!module) {
Registry.register("cylon-" + opts.driver);
module = Registry.findByDriver(opts.driver);
}
var driver = module.driver(opts);
_.forIn(driver, function(prop, name) {
if (name === "constructor") {
return;
}
if (_.isFunction(prop)) {
driver[name] = prop.bind(driver);
}
});
if (testMode()) {
var testDriver = Registry.findByDriver("test").driver(opts);
_.forIn(driver, function(prop, name) {
if (_.isFunction(prop) && !testDriver[name]) {
testDriver[name] = function() { return true; };
}
});
return testDriver;
}
return driver;
};

58
lib/initializer.js Normal file
View File

@ -0,0 +1,58 @@
/*
* Device/Connection Initializer
* cylonjs.com
*
* Copyright (c) 2013-2015 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
"use strict";
var Registry = require("./registry"),
Config = require("./config"),
_ = require("./lodash");
function testMode() {
return process.env.NODE_ENV === "test" && Config.testMode;
}
module.exports = function Initializer(type, opts) {
var mod;
if (opts.module) {
mod = Registry.register(opts.module);
} else {
mod = Registry.findBy(type, opts[type]);
}
if (!module) {
Registry.register("cylon-" + opts[type]);
mod = Registry.findBy(type, opts[type]);
}
var obj = mod[type](opts);
_.forIn(obj, function(prop, name) {
if (name === "constructor") {
return;
}
if (_.isFunction(prop)) {
obj[name] = prop.bind(obj);
}
});
if (testMode()) {
var test = Registry.findBy(type, "test")[type](opts);
_.forIn(obj, function(prop, name) {
if (_.isFunction(prop) && !test[name]) {
test[name] = function() { return true; };
}
});
return test;
}
return obj;
};

View File

@ -66,12 +66,13 @@ var Registry = module.exports = {
return this.data[module].module;
},
findByAdaptor: function(adaptor) {
return this.search("adaptors", adaptor);
},
findBy: function(prop, name) {
// pluralize, if necessary
if (!/s$/.test(name)) {
prop += "s";
}
findByDriver: function(driver) {
return this.search("drivers", driver);
return this.search(prop, name);
},
findByModule: function(module) {

View File

@ -8,8 +8,7 @@
"use strict";
var initConnection = require("./connection"),
initDevice = require("./device"),
var initializer = require("./initializer"),
Logger = require("./logger"),
Utils = require("./utils"),
Config = require("./config"),
@ -117,7 +116,7 @@ Robot.prototype.connection = function(name, conn) {
this.log("warn", str);
}
this.connections[conn.name] = initConnection(conn);
this.connections[conn.name] = initializer("adaptor", conn);
return this;
};
@ -221,7 +220,7 @@ Robot.prototype.device = function(name, device) {
device.connection = _.first(_.values(this.connections));
}
this.devices[device.name] = initDevice(device);
this.devices[device.name] = initializer("driver", device);
return this;
};

View File

@ -1,16 +0,0 @@
/* jshint expr:true */
"use strict";
var Loopback = source("test/loopback"),
connection = source("connection");
describe("Connection", function() {
it("returns a Adaptor instance", function() {
var conn = connection({
name: "test",
adaptor: "loopback"
});
expect(conn).to.be.an.instanceOf(Loopback);
});
});

View File

@ -1,16 +0,0 @@
/* jshint expr:true */
"use strict";
var Ping = source("test/ping"),
device = source("device");
describe("Device", function() {
it("returns a Driver instance", function() {
var driver = device({
name: "test",
driver: "ping"
});
expect(driver).to.be.an.instanceOf(Ping);
});
});

View File

@ -35,24 +35,4 @@ describe("Registry", function() {
});
});
});
describe("#findByAdaptor", function() {
beforeEach(function() {
Registry.register(path);
});
it("finds the appropriate module containing the adaptor", function() {
expect(Registry.findByAdaptor("test-adaptor")).to.be.eql(mod);
});
});
describe("#findByDriver", function() {
beforeEach(function() {
Registry.register(path);
});
it("finds the appropriate module containing the driver", function() {
expect(Registry.findByDriver("test-driver")).to.be.eql(mod);
});
});
});