cylon/lib/initializer.js

59 lines
1.1 KiB
JavaScript
Raw Normal View History

/*
* 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;
if (opts.module) {
mod = Registry.register(opts.module);
} else {
mod = Registry.findBy(type, opts[type]);
}
if (!mod) {
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;
};