From a7e8caf19df742d446faae56837087e8568b79af Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 17 Jan 2015 15:48:45 -0800 Subject: [PATCH 1/3] Extract robot var init into separate function --- lib/robot.js | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/lib/robot.js b/lib/robot.js index d45ef34..2d46660 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -27,19 +27,6 @@ var Async = require("async"), // work - work to be performed when the Robot is started // // Returns a new Robot -// Example (CoffeeScript): -// Cylon.robot -// name: "Spherobot!" -// -// connection: -// name: "sphero", adaptor: "sphero", port: "/dev/rfcomm0" -// -// device: -// name: "sphero", driver: "sphero" -// -// work: (me) -> -// Utils.every 1.second(), -> -// me.sphero.roll 60, Math.floor(Math.random() * 360// var Robot = module.exports = function Robot(opts) { opts = opts || {}; @@ -49,25 +36,14 @@ var Robot = module.exports = function Robot(opts) { "startDevices", "startConnections", "start", + "initRobot", "initDevices", "initConnections" ]; _.bindAll(this, methods); - this.name = opts.name || Robot.randomName(); - this.connections = {}; - this.devices = {}; - this.adaptors = {}; - this.drivers = {}; - this.commands = {}; - this.running = false; - this.work = opts.work || opts.play; - - if (!this.work) { - this.work = function() { Logger.debug("No work yet."); }; - } - + this.initRobot(opts); this.initConnections(opts); this.initDevices(opts); @@ -145,6 +121,26 @@ Robot.prototype.connection = function(name, conn) { return this; }; +// Public: Initializes all vars for robot +// +// opts - options array passed to constructor +// +// Returns null +Robot.prototype.initRobot = function(opts) { + this.name = opts.name || Robot.randomName(); + this.connections = {}; + this.devices = {}; + this.adaptors = {}; + this.drivers = {}; + this.commands = {}; + this.running = false; + this.work = opts.work || opts.play; + + if (!this.work) { + this.work = function() { Logger.debug("No work yet."); }; + } +}; + // Public: Initializes all connections for the robot // // opts - options array passed to constructor From 5cea07602a86fcef244b769912e224f9f6a036c5 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 17 Jan 2015 21:29:07 -0800 Subject: [PATCH 2/3] Extract function to DRY up code --- lib/robot.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/robot.js b/lib/robot.js index 2d46660..b62ba0c 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -172,11 +172,7 @@ Robot.prototype.initConnections = function(opts) { Logger.warn(str); - _.forEach(opts.connections, function(conn, key) { - var name = _.isString(key) ? key : conn.name; - this.connection(name, conn); - }, this); - + this.performArraySetup(opts.connections, "connection"); return this.connections; } @@ -269,11 +265,7 @@ Robot.prototype.initDevices = function(opts) { Logger.warn(str); - _.forEach(opts.devices, function(device, key) { - var name = _.isString(key) ? key : device.name; - this.device(name, device); - }, this); - + this.performArraySetup(opts.devices, "device"); return this.devices; } @@ -421,3 +413,10 @@ Robot.prototype.halt = function(callback) { Robot.prototype.toString = function() { return "[Robot name='" + this.name + "']"; }; + +Robot.prototype.performArraySetup = function(things, typeOfThing) { + _.forEach(things, function(t, key) { + var name = _.isString(key) ? key : t.name; + this[typeOfThing](name, t); + }, this); +}; From 6ac3cad7c146abed4e0271e7aa44614ca75e61c4 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 17 Jan 2015 21:35:40 -0800 Subject: [PATCH 3/3] Extract even more of function to DRY up code --- lib/robot.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/robot.js b/lib/robot.js index b62ba0c..b672e4a 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -167,12 +167,7 @@ Robot.prototype.initConnections = function(opts) { if (_.isObject(opts.connections)) { if (_.isArray(opts.connections)) { - str = "Specifying connections as an array is deprecated. "; - str += "It will be removed in 1.0.0."; - - Logger.warn(str); - - this.performArraySetup(opts.connections, "connection"); + this.performArraySetup(opts.connections, "connection", "connections"); return this.connections; } @@ -260,12 +255,7 @@ Robot.prototype.initDevices = function(opts) { if (_.isObject(opts.devices)) { if (_.isArray(opts.devices)) { - str = "Specifying devices as an array is deprecated. "; - str += "It will be removed in 1.0.0."; - - Logger.warn(str); - - this.performArraySetup(opts.devices, "device"); + this.performArraySetup(opts.devices, "device", "devices"); return this.devices; } @@ -414,7 +404,14 @@ Robot.prototype.toString = function() { return "[Robot name='" + this.name + "']"; }; -Robot.prototype.performArraySetup = function(things, typeOfThing) { +Robot.prototype.performArraySetup = function(things, typeOfThing, arrayName) { + var str = "Specifying "; + str += arrayName; + str += " as an array is deprecated. "; + str += "It will be removed in 1.0.0."; + + Logger.warn(str); + _.forEach(things, function(t, key) { var name = _.isString(key) ? key : t.name; this[typeOfThing](name, t);