2015-01-23 06:40:09 +08:00
|
|
|
/*
|
|
|
|
* 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]);
|
|
|
|
}
|
|
|
|
|
2015-01-30 03:51:05 +08:00
|
|
|
if (!mod) {
|
2015-01-23 06:40:09 +08:00
|
|
|
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;
|
|
|
|
};
|