diff --git a/lib/robot.js b/lib/robot.js index 6349973..7d9ca3b 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -154,8 +154,9 @@ Robot.prototype.toJSON = function() { }; }; -Robot.prototype.connection = function(conn) { +Robot.prototype.connection = function(name, conn) { conn.robot = this; + conn.name = name; if (this.connections[conn.name]) { var original = conn.name; @@ -184,30 +185,29 @@ Robot.prototype.initConnections = function(opts) { if (opts.connection) { Logger.warn("Specifying a single connection with the 'connection' key is deprecated, and will be removed in 1.0.0."); - this.connection(opts.connection); + this.connection(opts.connection.name, opts.connection); return this.connections; } if (typeof opts.connections == 'object' && !isArray(opts.connections)) { for (var name in opts.connections) { - var conn = opts.connections[name]; - conn.name = name; - this.connection(conn); + this.connection(name, opts.connections[name]); } } if (isArray(opts.connections)) { Logger.warn("Specifying connections as an array is deprecated, and will be removed in 1.0.0."); opts.connections.forEach(function(conn) { - this.connection(conn); + this.connection(conn.name, conn); }.bind(this)); } return this.connections; }; -Robot.prototype.device = function(device) { +Robot.prototype.device = function(name, device) { device.robot = this; + device.name = name; if (this.devices[device.name]) { var original = device.name; @@ -256,22 +256,20 @@ Robot.prototype.initDevices = function(opts) { if (opts.device) { Logger.warn("Specifying a single device with the 'device' key is deprecated, and will be removed in 1.0.0."); - this.device(opts.device); + this.device(opts.device.name, opts.device); return this.devices; } if (typeof opts.devices == 'object' && !isArray(opts.devices)) { for (var name in opts.devices) { - var device = opts.devices[name]; - device.name = name; - this.device(device); + this.device(name, opts.devices[name]); } } if (isArray(opts.devices)) { Logger.warn("Specifying devices as an array is deprecated, and will be removed in 1.0.0."); opts.devices.forEach(function(device) { - this.device(device); + this.device(device.name, device); }.bind(this)); } diff --git a/spec/lib/robot.spec.js b/spec/lib/robot.spec.js index a26d7d5..20fd7fd 100644 --- a/spec/lib/robot.spec.js +++ b/spec/lib/robot.spec.js @@ -230,23 +230,23 @@ describe("Robot", function() { beforeEach(function() { bot = new Robot(); - opts = { name: 'loopback', adaptor: 'loopback' }; + opts = { adaptor: 'loopback' }; }); it("creates and adds a new Connection", function() { expect(bot.connections.loopback).to.be.eql(undefined); - bot.connection(opts); + bot.connection('loopback', opts); expect(bot.connections.loopback).to.be.an.instanceOf(Adaptor); }) it("sets @robot on the Connection to be the Robot initializing it", function() { - bot.connection(opts); + bot.connection('loopback', opts); expect(bot.connections.loopback.robot).to.be.eql(bot); }) it("avoids name collisions", function() { - bot.connection(opts); - bot.connection(opts); + bot.connection('loopback', opts); + bot.connection('loopback', opts); expect(Object.keys(bot.connections)).to.be.eql(['loopback', 'loopback-1']); }); }); @@ -301,23 +301,23 @@ describe("Robot", function() { beforeEach(function() { bot = new Robot(); - opts = { name: 'ping', driver: 'ping' }; + opts = { driver: 'ping' }; }); it("creates and adds a new Device", function() { expect(bot.devices.ping).to.be.eql(undefined); - bot.device(opts); + bot.device('ping', opts); expect(bot.devices.ping).to.be.an.instanceOf(Driver); }) it("sets @robot on the Device to be the Robot initializing it", function() { - bot.device(opts); + bot.device('ping', opts); expect(bot.devices.ping.robot).to.be.eql(bot); }) it("avoids name collisions", function() { - bot.device(opts); - bot.device(opts); + bot.device('ping', opts); + bot.device('ping', opts); expect(Object.keys(bot.devices)).to.be.eql(['ping', 'ping-1']); }); });