Code coverage report for hodman/lib/store/lookup.js

Statements: 17.24% (5 / 29)      Branches: 0% (0 / 4)      Functions: 0% (0 / 9)      Lines: 17.24% (5 / 29)      Ignored: none     

All files » hodman/lib/store/ » lookup.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 1341 1 1                   1                                                                                                                                                                                                                                               1  
var Base = require('preceptor-core').Base;
var _ = require('underscore');
var Promise = require('promise');
 
/**
 * @class StoreLookup
 * @extends Base
 *
 * @property {ServiceLookup} _service
 * @property {object} _stores
 * @property {object} _cache
 */
var StoreLookup = Base.extend(
 
	/**
	 * @constructor
	 * @param {ServiceLookup} service
	 */
	function (service) {
		this.__super();
 
		this._service = service;
		this._stores = {};
		this._cache = {};
 
		this.initialize();
	},
 
	{
		/**
		 * Initializes the store
		 *
		 * @method initialize
		 */
		initialize: function () {
			// Do nothing
		},
 
 
		/**
		 * Gets the api service object
		 *
		 * @method getService
		 * @return {ServiceLookup}
		 */
		getService: function () {
			return this._service;
		},
 
		/**
		 * Registers a store
		 *
		 * @method registerStore
		 * @param {function} Class
		 */
		registerStore: function (Class) {
 
			var keys = Class.keys;
 
			_.each(keys, function (key) {
				this._stores[key] = Class;
			}, this);
		},
 
 
		/**
		 * Lookup a store
		 *
		 * @method lookupStore
		 * @param {string} key
		 * @return {function}
		 */
		lookupStore: function (key) {
			return this._stores[key];
		},
 
		/**
		 * Retrieves a store
		 *
		 * @method getStore
		 * @param {string} key
		 * @return {StoreBase}
		 */
		getStore: function (key) {
 
			var Class = this.lookupStore(key),
				service,
				endPoint,
				cacheKey;
 
			if (!Class) {
				throw new Error('Store ' + key + ' could not be found.');
			}
			cacheKey = Class.keys[0];
 
			if (!this._cache[cacheKey]) {
				service = this.getService();
				endPoint = service.getEndPoint(key);
 
				this._cache[cacheKey] = new Class(this, endPoint);
			}
 
			return this._cache[cacheKey];
		},
 
 
		/**
		 * Deletes all records stored
		 *
		 * @return {Promise}
		 */
		deleteAllStored: function () {
			var promises = [];
 
			_.each(_.keys(this._cache), function (key) {
				var store = this._cache[key];
				promises.push(store.deleteAllStored());
			}, this);
 
			return Promise.all(promises);
		}
	},
	{
		/**
		 * @property TYPE
		 * @type {string}
		 * @static
		 */
		TYPE: 'StoreLookup'
	}
);
 
module.exports = StoreLookup;