Code coverage report for src/freight.js

Statements: 100% (52 / 52)      Branches: 100% (12 / 12)      Functions: 100% (13 / 13)      Lines: 100% (52 / 52)     

All files » src/ » freight.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 1691   1 23           23             23             23                 23 24 24 24   20     23                 23 85 19   66                     23 32 32               23 50 50 23   27                   23 2 2 8 5     2                   23 25 25 80 24       25                   23 24   24 1   23   20                       23 33   33 33                   23 9 9 30   9         1
var Definition = require('./definition.js');
 
var Freight = function() {
  var self = this;
 
  /**
   * Determin how service id are distinques of normal string arguments in configuration
   * @type {String}
   */
  self.idPrefix = ':';
 
  /**
   * the array of definitions this container managers
   * @type {Array}
   * @private
   */
  self._definitions = [];
 
  /**
   * the hash with parameters this container holds
   * @type {Object}
   * @private
   */
  self._parameters = {};
 
  /**
   * Get and service of parameter from the container
   *
   * @method getService()
   * @param  {string} id
   * @return {mixed}
   */
  self.get = function get(id) {
    var res = false;
    try {
      res = self.getParameter(id);  
    } catch(e) {
      res = self.getService(id);
    }
    
    return res;
  };
 
  /**
   * Check wether the string an parameter id
   * @method isId()
   * @param  {string}  str
   * @return {Boolean}
   */
  self.isId = function isId(val) {
    if(typeof val !== 'string')
      return false;
 
    return ((val.indexOf(self.idPrefix) === 0) ? (true) : (false));
  };
 
  /**
   * Set a parameter on this container
   * 
   * @method setParameter()
   * @param {string} id
   * @param {mixed} val
   * @chainable
   */
  self.setParameter = function setParameter(id, val) {
    self._parameters[id] = val;
    return self;
  };
 
  /**
   * Retrieve a parameter from the container
   * @param  {string} id
   * @return {mixed}
   */
  self.getParameter = function getParameter(id) {
    var res = self._parameters[id];
    if(res === undefined)
      throw new Error('Parameter "'+id+'" not found.');
 
    return res;
  };
 
  /**
   * Return an array of service ids from services with the given tag
   *
   * @method findTaggedServiceIds()
   * @param  {string} tag
   * @return {Array}
   */
  self.findTaggedServiceIds = function findTaggedServiceIds(tag) {
    var res = [];
    self._definitions.forEach(function(d){
      if(d.tags.indexOf(tag) !== -1) {
        res.push(d.id);
      }
    });
    return res;
  };
 
  /**
   * Return a service definition from the container
   *
   * @method getDefinition()
   * @param  {string} id
   * @return {Definition}
   */
  self.getDefinition = function getDefinition(id) {
    var def = false;
    self._definitions.forEach(function(d){
      if(d.id == id) {
        def = d;
      }
    });
 
    return def;
  };
 
  /**
   * Return an service by its id
   *
   * @method getService()
   * @param  {string} id
   * @return {mixed}
   */
  self.getService = function getService(id) {  
    var def = self.getDefinition(id);
 
    if(def === false)
      throw new Error('Service "'+id+'" not found.');
 
    var service = def.retrieve();
 
    return service;
  };
 
 
  /**
   * Register a new service as the specified id
   * 
   * @method register()
   * @param  {string}   id
   * @param  {Object} conf the configuration hash for the individual service
   * @return {Definition}
   */
  self.register = function register(id, conf) {
    var def = new Definition(id, conf, self);
 
    self._definitions.push(def);
    return def;
  };
 
  /**
   * Register several definitions from an configuration hash (e.g. json)
   * 
   * @method  registerAll()
   * @param  {Object} conf the services
   * @return  {Array} a array of registered definitions
   */
  self.registerAll = function build(conf) {
    var res = [];
    Object.keys(conf).forEach(function(id){
      res.push(self.register(id, conf[id]));
    });
    return res;
  };
 
};
 
module.exports = Freight;