src/freight.js
var Definition = require('./definition.js'); var Freight = function() { var self = this;

Determin how service id are distinques of normal string arguments in configuration

self.idPrefix = ':';

Private

the array of definitions this container managers

self._definitions = [];

Private

the hash with parameters this container holds

self._parameters = {};

Method getService()

Get and service of parameter from the container

Parameters:

  • id must be a string.

Returns a mixed

self.get = function get(id) { var res = false; try { res = self.getParameter(id); } catch(e) { res = self.getService(id); } return res; };

Method isId()

Check wether the string an parameter id

Parameters:

  • str must be a string.

Returns a Boolean

self.isId = function isId(val) { if(typeof val !== 'string') return false; return ((val.indexOf(self.idPrefix) === 0) ? (true) : (false)); };

Method setParameter()

Set a parameter on this container

Parameters:

  • id must be a string.

  • val must be a mixed.

self.setParameter = function setParameter(id, val) { self._parameters[id] = val; return self; };

Retrieve a parameter from the container

Parameters:

  • id must be a string.

Returns a mixed

self.getParameter = function getParameter(id) { var res = self._parameters[id]; if(res === undefined) throw new Error('Parameter "'+id+'" not found.'); return res; };

Method findTaggedServiceIds()

Return an array of service ids from services with the given tag

Parameters:

  • tag must be a string.

Returns an 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; };

Method getDefinition()

Return a service definition from the container

Parameters:

  • id must be a string.

Returns a Definition

self.getDefinition = function getDefinition(id) { var def = false; self._definitions.forEach(function(d){ if(d.id == id) { def = d; } }); return def; };

Method getService()

Return an service by its id

Parameters:

  • id must be a string.

Returns a 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; };

Method register()

Register a new service as the specified id

Parameters:

  • id must be a string.

  • conf must be an Object.
    (the configuration hash for the individual service)

Returns a Definition

self.register = function register(id, conf) { var def = new Definition(id, conf, self); self._definitions.push(def); return def; };

Method registerAll()

Register several definitions from an configuration hash (e.g. json)

Parameters:

  • conf must be an Object.
    (the services)

Returns an 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;