From 3b7c96e81e66e3322b39a7201e6e319dff1320d4 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Wed, 3 Sep 2014 15:04:36 -0700 Subject: [PATCH] Add user configuration for Cylon internals --- lib/config.js | 8 ++------ lib/cylon.js | 23 +++++++++++++++++++++++ lib/robot.js | 6 +++--- test/specs/cylon.spec.js | 37 ++++++++++++++++++++++++++++++++++++- 4 files changed, 64 insertions(+), 10 deletions(-) diff --git a/lib/config.js b/lib/config.js index b55cea5..15d98ce 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,5 +1,5 @@ /* - * cylon configuration loader + * Cylon - Internal Configuration * cylonjs.com * * Copyright (c) 2013-2014 The Hybrid Group @@ -8,8 +8,4 @@ 'use strict'; -var Utils = require('./utils'); - -module.exports = { - testing_mode: Utils.fetch(process.env, 'CYLON_TEST', false) -}; +module.exports = {}; diff --git a/lib/cylon.js b/lib/cylon.js index f03a6c9..a8c3775 100644 --- a/lib/cylon.js +++ b/lib/cylon.js @@ -12,6 +12,7 @@ var Async = require('async'); var Logger = require('./logger'), Robot = require('./robot'), + Config = require('./config'), Utils = require('./utils'); Logger.setup(); @@ -76,6 +77,28 @@ Cylon.start = function start() { } }; +// Public: Sets the internal configuration, based on passed options +// +// opts - object containing configuration key/value pairs +// +// Returns the updated config +Cylon.setConfig = function(opts) { + for (var o in opts) { + Config[o] = opts[o]; + } + + return Config; +}; + +// Public: Fetches a value from the internal configuration +// +// key - string key to be fetched from config +// +// Returns the config value +Cylon.config = function(key) { + return Config[key]; +}; + // Public: Halts the API and the robots // // callback - callback to be triggered when Cylon is ready to shutdown diff --git a/lib/robot.js b/lib/robot.js index b4d2004..c65b6cd 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -12,7 +12,7 @@ var Connection = require("./connection"), Device = require("./device"), Logger = require('./logger'), Utils = require('./utils'), - config = require('./config'); + Config = require('./config'); var Async = require("async"), EventEmitter = require('events').EventEmitter; @@ -317,7 +317,7 @@ Robot.prototype.initAdaptor = function(adaptorName, connection, opts) { extraParams: opts }); - if (config.testing_mode) { + if (Config.test_mode) { var testAdaptor = this.requireAdaptor('test').adaptor({ name: adaptorName, connection: connection, @@ -389,7 +389,7 @@ Robot.prototype.initDriver = function(driverName, device, opts) { extraParams: opts }); - if (config.testing_mode) { + if (Config.test_mode) { var testDriver = this.requireDriver('test').driver({ name: driverName, device: device, diff --git a/test/specs/cylon.spec.js b/test/specs/cylon.spec.js index 3166a38..673924c 100644 --- a/test/specs/cylon.spec.js +++ b/test/specs/cylon.spec.js @@ -6,7 +6,8 @@ var Cylon = source("cylon"), var API = source('api'), Logger = source('logger'), Adaptor = source('adaptor'), - Driver = source('driver'); + Driver = source('driver'), + Config = source('config'); describe("Cylon", function() { describe("exports", function() { @@ -86,6 +87,40 @@ describe("Cylon", function() { }); }); + describe("#setConfig", function() { + beforeEach(function() { + for (var c in Config) { + delete Config[c]; + } + }); + + it("sets config variables", function() { + Cylon.setConfig({ a: 1, b: 2 }); + expect(Config.a).to.be.eql(1); + expect(Config.b).to.be.eql(2); + }); + + it("updates existing config", function() { + Cylon.setConfig({ a: 1, b: 2 }); + Cylon.setConfig({ a: 3 }); + expect(Config.a).to.be.eql(3); + expect(Config.b).to.be.eql(2); + }); + + it("returns updated config", function() { + var config = Cylon.setConfig({ a: 1, b: 2 }); + expect(Config).to.be.eql(config); + }); + }); + + describe("#config", function() { + it("returns a value from the Config object", function() { + Config.a = "hello world"; + expect(Cylon.config("a")).to.be.eql("hello world"); + delete Config.a; + }) + }); + describe("#halt", function() { it("calls #halt() on all robots", function() { var bot1 = { halt: spy() },