From 9d6a19507be499e60c8bdbb716c47ab0d09ccbe7 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Sat, 25 Apr 2015 19:57:35 -0700 Subject: [PATCH] Add methods to manipulate Repl context --- lib/cylon.js | 6 +++--- lib/repl.js | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/cylon.js b/lib/cylon.js index 5e59662..9d133d8 100644 --- a/lib/cylon.js +++ b/lib/cylon.js @@ -150,13 +150,13 @@ Cylon.config = function(opts) { * @return {void} */ Cylon.repl = function repl() { - var repl = new Repl( + var instance = new Repl( { prompt: "mcp > " }, { robots: this.robots } ); - repl.start(); -} + instance.start(); +}; // Public: Halts the API and the robots // diff --git a/lib/repl.js b/lib/repl.js index 1d2ef96..5a2b9b2 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -53,3 +53,30 @@ Repl.prototype.start = function() { this.emit("reset"); }.bind(this)); }; + +// add a value to the context +Repl.prototype.addContext = function(key, value) { + this.context[key] = value; + this.repl.context[key] = value; +}; + +// remove a value from the context +Repl.prototype.removeContext = function(key) { + delete this.context[key]; + delete this.repl.context[key]; +}; + +// set the context to the provided object +Repl.prototype.setContext = function(object) { + _.each(this.context, function(value, key) { + if (this.context.hasOwnProperty(key)) { + this.removeContext(key); + } + }.bind(this)); + + _.each(object, function(value, key) { + if (object.hasOwnProperty(key)) { + this.addContext(key, value); + } + }.bind(this)); +};