Fluent #device/#connection update
Now pass name, then object of details #device({ name: 'ping', driver: 'ping' }); // vs #device('ping', { driver: 'ping' });
This commit is contained in:
parent
7d4aa1ffee
commit
ca8e22f1e2
22
lib/robot.js
22
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.robot = this;
|
||||||
|
conn.name = name;
|
||||||
|
|
||||||
if (this.connections[conn.name]) {
|
if (this.connections[conn.name]) {
|
||||||
var original = conn.name;
|
var original = conn.name;
|
||||||
|
@ -184,30 +185,29 @@ Robot.prototype.initConnections = function(opts) {
|
||||||
|
|
||||||
if (opts.connection) {
|
if (opts.connection) {
|
||||||
Logger.warn("Specifying a single connection with the 'connection' key is deprecated, and will be removed in 1.0.0.");
|
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;
|
return this.connections;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof opts.connections == 'object' && !isArray(opts.connections)) {
|
if (typeof opts.connections == 'object' && !isArray(opts.connections)) {
|
||||||
for (var name in opts.connections) {
|
for (var name in opts.connections) {
|
||||||
var conn = opts.connections[name];
|
this.connection(name, opts.connections[name]);
|
||||||
conn.name = name;
|
|
||||||
this.connection(conn);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isArray(opts.connections)) {
|
if (isArray(opts.connections)) {
|
||||||
Logger.warn("Specifying connections as an array is deprecated, and will be removed in 1.0.0.");
|
Logger.warn("Specifying connections as an array is deprecated, and will be removed in 1.0.0.");
|
||||||
opts.connections.forEach(function(conn) {
|
opts.connections.forEach(function(conn) {
|
||||||
this.connection(conn);
|
this.connection(conn.name, conn);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.connections;
|
return this.connections;
|
||||||
};
|
};
|
||||||
|
|
||||||
Robot.prototype.device = function(device) {
|
Robot.prototype.device = function(name, device) {
|
||||||
device.robot = this;
|
device.robot = this;
|
||||||
|
device.name = name;
|
||||||
|
|
||||||
if (this.devices[device.name]) {
|
if (this.devices[device.name]) {
|
||||||
var original = device.name;
|
var original = device.name;
|
||||||
|
@ -256,22 +256,20 @@ Robot.prototype.initDevices = function(opts) {
|
||||||
|
|
||||||
if (opts.device) {
|
if (opts.device) {
|
||||||
Logger.warn("Specifying a single device with the 'device' key is deprecated, and will be removed in 1.0.0.");
|
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;
|
return this.devices;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof opts.devices == 'object' && !isArray(opts.devices)) {
|
if (typeof opts.devices == 'object' && !isArray(opts.devices)) {
|
||||||
for (var name in opts.devices) {
|
for (var name in opts.devices) {
|
||||||
var device = opts.devices[name];
|
this.device(name, opts.devices[name]);
|
||||||
device.name = name;
|
|
||||||
this.device(device);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isArray(opts.devices)) {
|
if (isArray(opts.devices)) {
|
||||||
Logger.warn("Specifying devices as an array is deprecated, and will be removed in 1.0.0.");
|
Logger.warn("Specifying devices as an array is deprecated, and will be removed in 1.0.0.");
|
||||||
opts.devices.forEach(function(device) {
|
opts.devices.forEach(function(device) {
|
||||||
this.device(device);
|
this.device(device.name, device);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -230,23 +230,23 @@ describe("Robot", function() {
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
bot = new Robot();
|
bot = new Robot();
|
||||||
opts = { name: 'loopback', adaptor: 'loopback' };
|
opts = { adaptor: 'loopback' };
|
||||||
});
|
});
|
||||||
|
|
||||||
it("creates and adds a new Connection", function() {
|
it("creates and adds a new Connection", function() {
|
||||||
expect(bot.connections.loopback).to.be.eql(undefined);
|
expect(bot.connections.loopback).to.be.eql(undefined);
|
||||||
bot.connection(opts);
|
bot.connection('loopback', opts);
|
||||||
expect(bot.connections.loopback).to.be.an.instanceOf(Adaptor);
|
expect(bot.connections.loopback).to.be.an.instanceOf(Adaptor);
|
||||||
})
|
})
|
||||||
|
|
||||||
it("sets @robot on the Connection to be the Robot initializing it", function() {
|
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);
|
expect(bot.connections.loopback.robot).to.be.eql(bot);
|
||||||
})
|
})
|
||||||
|
|
||||||
it("avoids name collisions", function() {
|
it("avoids name collisions", function() {
|
||||||
bot.connection(opts);
|
bot.connection('loopback', opts);
|
||||||
bot.connection(opts);
|
bot.connection('loopback', opts);
|
||||||
expect(Object.keys(bot.connections)).to.be.eql(['loopback', 'loopback-1']);
|
expect(Object.keys(bot.connections)).to.be.eql(['loopback', 'loopback-1']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -301,23 +301,23 @@ describe("Robot", function() {
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
bot = new Robot();
|
bot = new Robot();
|
||||||
opts = { name: 'ping', driver: 'ping' };
|
opts = { driver: 'ping' };
|
||||||
});
|
});
|
||||||
|
|
||||||
it("creates and adds a new Device", function() {
|
it("creates and adds a new Device", function() {
|
||||||
expect(bot.devices.ping).to.be.eql(undefined);
|
expect(bot.devices.ping).to.be.eql(undefined);
|
||||||
bot.device(opts);
|
bot.device('ping', opts);
|
||||||
expect(bot.devices.ping).to.be.an.instanceOf(Driver);
|
expect(bot.devices.ping).to.be.an.instanceOf(Driver);
|
||||||
})
|
})
|
||||||
|
|
||||||
it("sets @robot on the Device to be the Robot initializing it", function() {
|
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);
|
expect(bot.devices.ping.robot).to.be.eql(bot);
|
||||||
})
|
})
|
||||||
|
|
||||||
it("avoids name collisions", function() {
|
it("avoids name collisions", function() {
|
||||||
bot.device(opts);
|
bot.device('ping', opts);
|
||||||
bot.device(opts);
|
bot.device('ping', opts);
|
||||||
expect(Object.keys(bot.devices)).to.be.eql(['ping', 'ping-1']);
|
expect(Object.keys(bot.devices)).to.be.eql(['ping', 'ping-1']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue