Add methods to manipulate Repl context

This commit is contained in:
Andrew Stewart 2015-04-25 19:57:35 -07:00
parent 2ff7dc0866
commit 9d6a19507b
2 changed files with 30 additions and 3 deletions

View File

@ -150,13 +150,13 @@ Cylon.config = function(opts) {
* @return {void} * @return {void}
*/ */
Cylon.repl = function repl() { Cylon.repl = function repl() {
var repl = new Repl( var instance = new Repl(
{ prompt: "mcp > " }, { prompt: "mcp > " },
{ robots: this.robots } { robots: this.robots }
); );
repl.start(); instance.start();
} };
// Public: Halts the API and the robots // Public: Halts the API and the robots
// //

View File

@ -53,3 +53,30 @@ Repl.prototype.start = function() {
this.emit("reset"); this.emit("reset");
}.bind(this)); }.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));
};