Consolidate Robot initialization to a single fn
Additionally, extracts validation of connections being present out to the validator logic.
This commit is contained in:
parent
1726cdb5f1
commit
ceac0df8dd
49
lib/robot.js
49
lib/robot.js
|
@ -39,8 +39,6 @@ var Robot = module.exports = function Robot(opts) {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.initRobot(opts);
|
this.initRobot(opts);
|
||||||
this.initConnections(opts);
|
|
||||||
this.initDevices(opts);
|
|
||||||
|
|
||||||
_.each(opts, function(opt, name) {
|
_.each(opts, function(opt, name) {
|
||||||
if (this[name] !== undefined) {
|
if (this[name] !== undefined) {
|
||||||
|
@ -148,20 +146,6 @@ Robot.prototype.initRobot = function(opts) {
|
||||||
if (!this.work) {
|
if (!this.work) {
|
||||||
this.work = function() { this.log("debug", "No work yet."); };
|
this.work = function() { this.log("debug", "No work yet."); };
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a Robot's connections
|
|
||||||
*
|
|
||||||
* @param {Object} opts object passed to constructor
|
|
||||||
* @return {Array} the Robot's connections
|
|
||||||
*/
|
|
||||||
Robot.prototype.initConnections = function(opts) {
|
|
||||||
this.log("info", "Initializing connections.");
|
|
||||||
|
|
||||||
if (opts.connections == null) {
|
|
||||||
return this.connections;
|
|
||||||
}
|
|
||||||
|
|
||||||
_.each(opts.connections, function(conn, key) {
|
_.each(opts.connections, function(conn, key) {
|
||||||
var name = _.isString(key) ? key : conn.name;
|
var name = _.isString(key) ? key : conn.name;
|
||||||
|
@ -177,10 +161,13 @@ Robot.prototype.initConnections = function(opts) {
|
||||||
delete conn.devices;
|
delete conn.devices;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.connection(name, conn);
|
this.connection(name, _.extend({}, conn));
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
return this.connections;
|
_.each(opts.devices, function(device, key) {
|
||||||
|
var name = _.isString(key) ? key : device.name;
|
||||||
|
this.device(name, _.extend({}, device));
|
||||||
|
}, this);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -225,32 +212,6 @@ Robot.prototype.device = function(name, device) {
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a Robot's devices
|
|
||||||
*
|
|
||||||
* @param {Object} opts object passed to constructor
|
|
||||||
* @return {Array} the Robot's devices
|
|
||||||
*/
|
|
||||||
Robot.prototype.initDevices = function(opts) {
|
|
||||||
this.log("info", "Initializing devices.");
|
|
||||||
|
|
||||||
if (opts.devices == null) {
|
|
||||||
return this.devices;
|
|
||||||
}
|
|
||||||
|
|
||||||
// check that there are connections to use
|
|
||||||
if (!Object.keys(this.connections).length) {
|
|
||||||
throw new Error("No connections specified");
|
|
||||||
}
|
|
||||||
|
|
||||||
_.each(opts.devices, function(device, key) {
|
|
||||||
var name = _.isString(key) ? key : device.name;
|
|
||||||
this.device(name, device);
|
|
||||||
}, this);
|
|
||||||
|
|
||||||
return this.devices;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the Robot's connections, then devices, then work.
|
* Starts the Robot's connections, then devices, then work.
|
||||||
*
|
*
|
||||||
|
|
|
@ -84,6 +84,15 @@ checks.devicesWithoutConnection = function(opts) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
checks.noConnections = function(opts) {
|
||||||
|
var connections = Object.keys(opts.connections || {}).length,
|
||||||
|
devices = Object.keys(opts.devices || {}).length;
|
||||||
|
|
||||||
|
if (devices && !connections) {
|
||||||
|
fatal(["No connections provided for devices"]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports.validate = function validate(opts) {
|
module.exports.validate = function validate(opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,7 @@ describe("Robot", function() {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(fn).to.throw(Error, "No connections specified");
|
expect(fn).to.throw(Error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -264,27 +264,20 @@ describe("Robot", function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("initConnections", function() {
|
describe("initRobot", function() {
|
||||||
var bot;
|
var bot;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
bot = new Robot();
|
bot = new Robot();
|
||||||
});
|
});
|
||||||
|
|
||||||
context("when not passed anything", function() {
|
context("when connection details are provided", function() {
|
||||||
it("does not modify the bot's connections", function() {
|
|
||||||
bot.initConnections({});
|
|
||||||
expect(bot.connections).to.be.eql({});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
context("when passed an object containing connection details", function() {
|
|
||||||
it("creates new connections with each of the ones provided", function() {
|
it("creates new connections with each of the ones provided", function() {
|
||||||
var connections = {
|
var connections = {
|
||||||
loopback: { adaptor: "loopback" }
|
loopback: { adaptor: "loopback" }
|
||||||
};
|
};
|
||||||
|
|
||||||
bot.initConnections({ connections: connections });
|
bot.initRobot({ connections: connections });
|
||||||
expect(bot.connections.loopback).to.be.instanceOf(Adaptor);
|
expect(bot.connections.loopback).to.be.instanceOf(Adaptor);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -303,7 +296,7 @@ describe("Robot", function() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
bot.initConnections(opts);
|
bot.initRobot(opts);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("adds the devices to opts.devices", function() {
|
it("adds the devices to opts.devices", function() {
|
||||||
|
@ -317,6 +310,23 @@ describe("Robot", function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
context("when device details are provided", function() {
|
||||||
|
it("creates new devices with each of the ones provided", function() {
|
||||||
|
var opts = {
|
||||||
|
connections: {
|
||||||
|
loopback: { adaptor: "loopback" }
|
||||||
|
},
|
||||||
|
|
||||||
|
devices: {
|
||||||
|
ping: { driver: "ping" }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
bot.initRobot(opts);
|
||||||
|
expect(bot.devices.ping).to.be.instanceOf(Driver);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("#device", function() {
|
describe("#device", function() {
|
||||||
|
@ -345,34 +355,7 @@ describe("Robot", function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("initDevices", function() {
|
describe("initRobot", function() {
|
||||||
var bot;
|
|
||||||
|
|
||||||
beforeEach(function() {
|
|
||||||
bot = new Robot({
|
|
||||||
connections: {
|
|
||||||
loopback: { adaptor: "loopback" }
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
context("when not passed anything", function() {
|
|
||||||
it("does not modify the bot's devices", function() {
|
|
||||||
bot.initDevices({});
|
|
||||||
expect(bot.devices).to.be.eql({});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
context("when passed an object containing device details", function() {
|
|
||||||
it("creates new devices with each of the ones provided", function() {
|
|
||||||
var devices = {
|
|
||||||
ping: { driver: "ping" }
|
|
||||||
};
|
|
||||||
|
|
||||||
bot.initDevices({ devices: devices });
|
|
||||||
expect(bot.devices.ping).to.be.instanceOf(Driver);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("#start", function() {
|
describe("#start", function() {
|
||||||
|
|
Loading…
Reference in New Issue