Plato on Github
Report Home
lib/di_container.js
Maintainability
60.89
Lines of code
168
Difficulty
53.56
Estimated Errors
1.28
Function weight
By Complexity
By SLOC
// Generated by CoffeeScript 1.6.2 /* This is DI Container for Clinch - its make available DI and simplify configuration. */ (function() { var BundleProcessor, DIContainer, FileLoader, FileProcessor, Gatherer, Packer, _, __hasProp = {}.hasOwnProperty, __slice = [].slice; _ = require('lodash'); Packer = require('./packer'); Gatherer = require('./gatherer'); FileLoader = require('./file_loader'); FileProcessor = require('./file_processor'); BundleProcessor = require('./bundle_processor'); DIContainer = (function() { function DIContainer() { this._packer_ = null; this._gatherer_ = null; this._file_loader_ = null; this._file_processor_ = null; this._bundle_processor_ = null; this._component_settings_ = this._initComponentSetting(); } /* This method simple setter for components settings its wipe out old values chainable */ DIContainer.prototype.setComponentsSettings = function(options) { var key, up_key, value; if (options == null) { options = {}; } for (key in options) { if (!__hasProp.call(options, key)) continue; value = options[key]; up_key = this._upperCaseWithExistenceCheck(key); this._component_settings_[up_key] = value; } this._flushComponentsChache(); return this; }; /* This method add some deep settings to components settings only overwrite old deep settings, but not all key chainable */ DIContainer.prototype.addComponentsSettings = function() { var component_name, deep_walker, last_path_idx, new_value_obj, path, up_key, _i; component_name = arguments[0], path = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), new_value_obj = arguments[_i++]; up_key = this._upperCaseWithExistenceCheck(component_name); last_path_idx = path.length - 1; deep_walker = function(accumulator, step_val, idx) { var key, value, _j, _len, _ref, _ref1, _results; if (!accumulator[step_val]) { accumulator[step_val] = {}; } if (idx === last_path_idx) { _ref = _.pairs(new_value_obj); _results = []; for (_j = 0, _len = _ref.length; _j < _len; _j++) { _ref1 = _ref[_j], key = _ref1[0], value = _ref1[1]; accumulator[step_val][key] = value; _results.push(null); } return _results; } else { return accumulator = accumulator[step_val]; } }; _.reduce(path, deep_walker, this._component_settings_[up_key]); this._flushComponentsChache(); return this; }; /* This method return component by it name may recursive resolve dependencies */ DIContainer.prototype.getComponent = function(component_name) { var settings, up_name; up_name = this._upperCaseWithExistenceCheck(component_name); settings = this._component_settings_[up_name]; switch (up_name) { case 'FILELOADER': return this._file_loader_ || (this._file_loader_ = new FileLoader(settings)); case 'FILEPROCESSOR': return this._file_processor_ || (this._file_processor_ = new FileProcessor(this.getComponent('FileLoader'), settings)); case 'GATHERER': return this._gatherer_ || (this._gatherer_ = new Gatherer(this.getComponent('FileProcessor'), settings)); case 'BUNDLEPROCESSOR': return this._bundle_processor_ || (this._bundle_processor_ = new BundleProcessor(this.getComponent('Gatherer'), settings)); case 'PACKER': return this._packer_ || (this._packer_ = new Packer(this.getComponent('BundleProcessor'), settings)); default: throw Error("418! don't know component |" + component_name + "|"); } }; /* This internal method to flush all objects cache used if settings changed YES, its copy-paste, but we are MUST to declare all object properties in constructor */ DIContainer.prototype._flushComponentsChache = function() { this._packer_ = null; this._gatherer_ = null; this._file_loader_ = null; this._file_processor_ = null; this._bundle_processor_ = null; return null; }; /* This internal method to check is uppercased name exists in @_component_settings_ return uppercased name or throw error */ DIContainer.prototype._upperCaseWithExistenceCheck = function(key) { var up_key; up_key = key.toUpperCase(); if (!this._component_settings_[up_key]) { throw Error("don't know component name |" + key + "|, mistype?"); } return up_key; }; /* Internal initor to reduce constructor */ DIContainer.prototype._initComponentSetting = function() { return { PACKER: {}, GATHERER: {}, FILELOADER: {}, FILEPROCESSOR: {}, BUNDLEPROCESSOR: {} }; }; return DIContainer; })(); module.exports = DIContainer; }).call(this);