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

Statements: 20.83% (5 / 24)      Branches: 0% (0 / 6)      Functions: 0% (0 / 9)      Lines: 20.83% (5 / 24)      Ignored: none     

All files » hodman/lib/store/ » base.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 1361 1 1                   1                                                                                                                                                                                                                                                   1  
var Base = require('preceptor-core').Base;
var _ = require('underscore');
var Promise = require('promise');
 
/**
 * @class StoreBase
 * @extends Base
 *
 * @property {object} _cache
 * @property {Store} _store
 * @property {ServiceEndPoint} _endPoint
 */
var StoreBase = Base.extend(
 
	/**
	 * @constructor
	 * @param {Store} store
	 * @param {ServiceEndPoint} endPoint
	 */
	function (store, endPoint) {
		this.__super();
 
		this._cache = {};
		this._store = store;
		this._endPoint = endPoint;
	},
 
	{
		/**
		 * Identifier prefix
		 *
		 * @property prefix
		 * @type {string}
		 */
		prefix: '',
 
 
		/**
		 * Gets the cache object
		 *
		 * @method getCache
		 * @return {object}
		 */
		getCache: function () {
			return this._cache;
		},
 
		/**
		 * Gets the store-lookup object
		 *
		 * @method getStoreLookup
		 * @return {Store}
		 */
		getStoreLookup: function () {
			return this._store;
		},
 
		/**
		 * Gets the end-point object
		 *
		 * @method getEndPoint
		 * @return {ServiceEndPoint}
		 */
		getEndPoint: function () {
			return this._endPoint;
		},
 
 
		/**
		 * Loads or creates a store object
		 *
		 * @method retrieve
		 * @param {string} id
		 * @param {object} [options]
		 * @return {Promise} With {ServiceEntry}
		 */
		retrieve: function (id, options) {
			if (this._cache[id]) {
				return Promise.resolve(this._cache[id]);
			} else {
				return this[id](options || {}).then(function (value) {
					this._cache[id] = value;
					return value;
				}.bind(this));
			}
		},
 
		/**
		 * Generates a unique identifier
		 *
		 * @method generateUniqueIdentifier
		 * @param {string} prefix
		 */
		generateUniqueIdentifier: function (prefix) {
			var id = Date.now();
			return (prefix || "") + this.prefix + id + "";
		},
 
		/**
		 * Deletes all records stored
		 *
		 * @return {Promise}
		 */
		deleteAllStored: function () {
			var endPoint = this.getEndPoint(),
				promises = [];
 
			_.each(_.keys(this._cache), function (key) {
				var record = this._cache[key],
					id = record.getValue('id');
 
				promises.push(endPoint.removeItem(id));
			}, this);
 
			return Promise.all(promises);
		}
	},
	{
		/**
		 * @property TYPE
		 * @type {string}
		 * @static
		 */
		TYPE: 'StoreBase',
 
		/**
		 * @property keys
		 * @type {string[]}
		 * @static
		 */
		keys: null
	}
);
 
module.exports = StoreBase;