Add exploding interface functions for Driver/Adaptor
Explicit is better than implicit, and this causes errors to be more specific
This commit is contained in:
parent
af5300f6cd
commit
82ef3d31a2
|
@ -12,6 +12,10 @@ var Basestar = require("./basestar"),
|
||||||
Utils = require("./utils"),
|
Utils = require("./utils"),
|
||||||
_ = require("./utils/helpers");
|
_ = require("./utils/helpers");
|
||||||
|
|
||||||
|
function formatErrorMessage(name, message) {
|
||||||
|
return ["Error in connection", "'" + name + "'", "- " + message].join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
// Public: Creates a new Adaptor
|
// Public: Creates a new Adaptor
|
||||||
//
|
//
|
||||||
// opts - hash of acceptable params
|
// opts - hash of acceptable params
|
||||||
|
@ -43,6 +47,30 @@ var Adaptor = module.exports = function Adaptor(opts) {
|
||||||
|
|
||||||
Utils.subclass(Adaptor, Basestar);
|
Utils.subclass(Adaptor, Basestar);
|
||||||
|
|
||||||
|
// Public: Basic #connect function. Must be overwritten by a descendent class
|
||||||
|
//
|
||||||
|
// Returns nothing, throws an error
|
||||||
|
Adaptor.prototype.connect = function() {
|
||||||
|
var message = formatErrorMessage(
|
||||||
|
this.name,
|
||||||
|
"Adaptor#connect method must be overwritten by descendant classes."
|
||||||
|
);
|
||||||
|
|
||||||
|
throw new Error(message);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Public: Basic #disconnect function. Must be overwritten by a descendent class
|
||||||
|
//
|
||||||
|
// Returns nothing, throws an error
|
||||||
|
Adaptor.prototype.disconnect = function() {
|
||||||
|
var message = formatErrorMessage(
|
||||||
|
this.name,
|
||||||
|
"Adaptor#disconnect method must be overwritten by descendant classes."
|
||||||
|
);
|
||||||
|
|
||||||
|
throw new Error(message);
|
||||||
|
};
|
||||||
|
|
||||||
// Public: Expresses the Connection in JSON format
|
// Public: Expresses the Connection in JSON format
|
||||||
//
|
//
|
||||||
// Returns an Object containing Connection data
|
// Returns an Object containing Connection data
|
||||||
|
|
|
@ -12,6 +12,10 @@ var Basestar = require("./basestar"),
|
||||||
Utils = require("./utils"),
|
Utils = require("./utils"),
|
||||||
_ = require("./utils/helpers");
|
_ = require("./utils/helpers");
|
||||||
|
|
||||||
|
function formatErrorMessage(name, message) {
|
||||||
|
return ["Error in driver", "'" + name + "'", "- " + message].join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
// Public: Creates a new Driver
|
// Public: Creates a new Driver
|
||||||
//
|
//
|
||||||
// opts - hash of acceptable params
|
// opts - hash of acceptable params
|
||||||
|
@ -47,6 +51,30 @@ var Driver = module.exports = function Driver(opts) {
|
||||||
|
|
||||||
Utils.subclass(Driver, Basestar);
|
Utils.subclass(Driver, Basestar);
|
||||||
|
|
||||||
|
// Public: Basic #start function. Must be overwritten by a descendent class
|
||||||
|
//
|
||||||
|
// Returns nothing, throws an error
|
||||||
|
Driver.prototype.start = function() {
|
||||||
|
var message = formatErrorMessage(
|
||||||
|
this.name,
|
||||||
|
"Driver#start method must be overwritten by descendant classes."
|
||||||
|
);
|
||||||
|
|
||||||
|
throw new Error(message);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Public: Basic #halt function. Must be overwritten by a descendent class
|
||||||
|
//
|
||||||
|
// Returns nothing, throws an error
|
||||||
|
Driver.prototype.halt = function() {
|
||||||
|
var message = formatErrorMessage(
|
||||||
|
this.name,
|
||||||
|
"Driver#halt method must be overwritten by descendant classes."
|
||||||
|
);
|
||||||
|
|
||||||
|
throw new Error(message);
|
||||||
|
};
|
||||||
|
|
||||||
Driver.prototype.setupCommands = function(commands, proxy) {
|
Driver.prototype.setupCommands = function(commands, proxy) {
|
||||||
if (proxy == null) {
|
if (proxy == null) {
|
||||||
proxy = this.connection;
|
proxy = this.connection;
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
/* jshint expr:true */
|
/* jshint expr:true */
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var Adaptor = source("adaptor");
|
var Adaptor = source("adaptor"),
|
||||||
|
Utils = source("utils");
|
||||||
|
|
||||||
describe("Adaptor", function() {
|
describe("Adaptor", function() {
|
||||||
var adaptor;
|
var adaptor;
|
||||||
|
@ -15,4 +16,32 @@ describe("Adaptor", function() {
|
||||||
expect(adaptor.name).to.be.eql("adaptor");
|
expect(adaptor.name).to.be.eql("adaptor");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("#interface methods", function() {
|
||||||
|
var child;
|
||||||
|
|
||||||
|
var Child = function Child() {};
|
||||||
|
|
||||||
|
Utils.subclass(Child, Adaptor);
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
child = new Child();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("#connect", function() {
|
||||||
|
it("throws an error unless overwritten", function() {
|
||||||
|
expect(child.connect).to.throw();
|
||||||
|
child.connect = function() {};
|
||||||
|
expect(child.connect).to.not.throw();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("#disconnect", function() {
|
||||||
|
it("throws an error unless overwritten", function() {
|
||||||
|
expect(child.disconnect).to.throw();
|
||||||
|
child.disconnect = function() {};
|
||||||
|
expect(child.disconnect).to.not.throw();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
/* jshint expr:true */
|
/* jshint expr:true */
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var Driver = source("driver");
|
var Driver = source("driver"),
|
||||||
|
Utils = source("utils");
|
||||||
|
|
||||||
describe("Driver", function() {
|
describe("Driver", function() {
|
||||||
var connection, driver;
|
var connection, driver;
|
||||||
|
@ -47,6 +48,34 @@ describe("Driver", function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("#interface methods", function() {
|
||||||
|
var child;
|
||||||
|
|
||||||
|
var Child = function Child() {};
|
||||||
|
|
||||||
|
Utils.subclass(Child, Driver);
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
child = new Child();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("#start", function() {
|
||||||
|
it("throws an error unless overwritten", function() {
|
||||||
|
expect(child.start).to.throw();
|
||||||
|
child.start = function() {};
|
||||||
|
expect(child.start).to.not.throw();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("#halt", function() {
|
||||||
|
it("throws an error unless overwritten", function() {
|
||||||
|
expect(child.halt).to.throw();
|
||||||
|
child.halt = function() {};
|
||||||
|
expect(child.halt).to.not.throw();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("#toJSON", function() {
|
describe("#toJSON", function() {
|
||||||
var driver, json;
|
var driver, json;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue