cylon/lib/initializer.js

63 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"),
2015-02-20 09:23:41 +08:00
Config = require("./config");
function testMode() {
return process.env.NODE_ENV === "test" && Config.testMode;
}
module.exports = function Initializer(type, opts) {
2015-02-20 09:23:41 +08:00
var mod, name, prop;
2015-02-20 04:44:35 +08:00
mod = Registry.findBy(type, opts[type]);
if (!mod) {
2015-02-20 04:44:35 +08:00
if (opts.module) {
Registry.register(opts.module);
} else {
Registry.register("cylon-" + opts[type]);
}
mod = Registry.findBy(type, opts[type]);
}
var obj = mod[type](opts);
2015-02-20 09:23:41 +08:00
for (name in obj) {
prop = obj[name];
if (name === "constructor") {
2015-02-20 09:23:41 +08:00
continue;
}
2015-02-20 09:23:41 +08:00
if (typeof prop === "function") {
obj[name] = prop.bind(obj);
}
2015-02-20 09:23:41 +08:00
}
if (testMode()) {
var test = Registry.findBy(type, "test")[type](opts);
2015-02-20 09:23:41 +08:00
for (name in obj) {
prop = obj[name];
if (typeof prop === "function" && !test[name]) {
test[name] = function() { return true; };
}
2015-02-20 09:23:41 +08:00
}
return test;
}
return obj;
};