cylon/lib/initializer.js

60 lines
1.1 KiB
JavaScript

/*
* Device/Connection Initializer
* cylonjs.com
*
* Copyright (c) 2013-2015 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
"use strict";
var Registry = require("./registry"),
Config = require("./config"),
_ = require("./lodash");
function testMode() {
return process.env.NODE_ENV === "test" && Config.testMode;
}
module.exports = function Initializer(type, opts) {
var mod;
mod = Registry.findBy(type, opts[type]);
if (!mod) {
if (opts.module) {
Registry.register(opts.module);
} else {
Registry.register("cylon-" + opts[type]);
}
mod = Registry.findBy(type, opts[type]);
}
var obj = mod[type](opts);
_.forIn(obj, function(prop, name) {
if (name === "constructor") {
return;
}
if (_.isFunction(prop)) {
obj[name] = prop.bind(obj);
}
});
if (testMode()) {
var test = Registry.findBy(type, "test")[type](opts);
_.forIn(obj, function(prop, name) {
if (_.isFunction(prop) && !test[name]) {
test[name] = function() { return true; };
}
});
return test;
}
return obj;
};