From 8065644b4e96ca43ebf6d3e92f13fac38876c4c8 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Wed, 7 Jan 2015 10:16:32 -0800 Subject: [PATCH] Add support for nested device details inside conn e.g. connections: { loopback: { adaptor: "loopback", devices: { ping: { driver: "ping", pin: 1 } } } } In the event of a name collision between the `devices` hash and a nested device, the nested device will 'win'. --- lib/robot.js | 32 ++++++++++++++++++++++++++++++-- spec/lib/robot.spec.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/lib/robot.js b/lib/robot.js index 12d4a4d..3d54161 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -132,6 +132,7 @@ Robot.prototype.connection = function(name, conn) { if (this.connections[conn.name]) { var original = conn.name, str; + conn.name = Utils.makeUnique(original, Object.keys(this.connections)); str = "Connection names must be unique."; @@ -174,10 +175,30 @@ Robot.prototype.initConnections = function(opts) { str += "It will be removed in 1.0.0."; Logger.warn(str); + + _.forEach(opts.connections, function(conn, key) { + var name = _.isString(key) ? key : conn.name; + this.connection(name, conn); + }, this); + + return this.connections; } - _.forEach(opts.connections, function(conn, key) { + _.forIn(opts.connections, function(conn, key) { var name = _.isString(key) ? key : conn.name; + + if (conn.devices) { + _.forIn(conn.devices, function(device, deviceName) { + opts.devices = opts.devices || {}; + + device.connection = name; + + opts.devices[deviceName] = device; + }); + + delete conn.devices; + } + this.connection(name, conn); }, this); } @@ -251,9 +272,16 @@ Robot.prototype.initDevices = function(opts) { str += "It will be removed in 1.0.0."; Logger.warn(str); + + _.forEach(opts.devices, function(device, key) { + var name = _.isString(key) ? key : device.name; + this.device(name, device); + }, this); + + return this.devices; } - _.forEach(opts.devices, function(device, key) { + _.forIn(opts.devices, function(device, key) { var name = _.isString(key) ? key : device.name; this.device(name, device); }, this); diff --git a/spec/lib/robot.spec.js b/spec/lib/robot.spec.js index fa570fc..67d4666 100644 --- a/spec/lib/robot.spec.js +++ b/spec/lib/robot.spec.js @@ -310,6 +310,35 @@ describe("Robot", function() { bot.initConnections({ connections: connections }); expect(bot.connections.loopback).to.be.instanceOf(Adaptor); }); + + context("when the object contains device details", function() { + var opts; + + beforeEach(function() { + opts = { + connections: { + loopback: { + adaptor: "loopback", + devices: { + ping: { driver: "ping", pin: 1 } + } + } + } + }; + + bot.initConnections(opts); + }); + + it("adds the devices to opts.devices", function() { + expect(opts.devices).to.be.eql({ + ping: { driver: "ping", pin: 1, connection: "loopback" } + }); + }); + + it("removes the device details from optsconnections", function() { + expect(opts.connections.devices).to.be.eql(undefined); + }); + }); }); context("when passed an array of connection objects", function() {