/*1Copyright (c) 2012, Yahoo! Inc. All rights reserved.2Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.3*/45var Factory = require('../util/factory'),6factory = new Factory('store', __dirname, false);7/**8* An abstraction for keeping track of content against some keys (e.g.9* original source, instrumented source, coverage objects against file names).10* This class is both the base class as well as a factory for `Store` implementations.11*12* Usage13* -----14*15* var Store = require('istanbul').Store,16* store = Store.create('memory');17*18* //basic use19* store.set('foo', 'foo-content');20* var content = store.get('foo');21*22* //keys and values23* store.keys().forEach(function (key) {24* console.log(key + ':\n' + store.get(key);25* });26* if (store.hasKey('bar') { console.log(store.get('bar'); }27*28*29* //syntactic sugar30* store.setObject('foo', { foo: true });31* console.log(store.getObject('foo').foo);32*33* store.dispose();34*35* @class Store36* @constructor37* @module store38* @param {Object} options Optional. The options supported by a specific store implementation.39* @main store40*/41function Store(/* options */) {}4243//add register, create, mix, loadAll, getStoreList as class methods44factory.bindClassMethods(Store);4546/**47* registers a new store implementation.48* @method register49* @static50* @param {Function} constructor the constructor function for the store. This function must have a51* `TYPE` property of type String, that will be used in `Store.create()`52*/53/**54* returns a store implementation of the specified type.55* @method create56* @static57* @param {String} type the type of store to create58* @param {Object} opts Optional. Options specific to the store implementation59* @return {Store} a new store of the specified type60*/6162Store.prototype = {63/**64* sets some content associated with a specific key. The manner in which65* duplicate keys are handled for multiple `set()` calls with the same66* key is implementation-specific.67*68* @method set69* @param {String} key the key for the content70* @param {String} contents the contents for the key71*/72set: function (/* key, contents */) { throw new Error("set: must be overridden"); },73/**74* returns the content associated to a specific key or throws if the key75* was not `set`76* @method get77* @param {String} key the key for which to get the content78* @return {String} the content for the specified key79*/80get: function (/* key */) { throw new Error("get: must be overridden"); },81/**82* returns a list of all known keys83* @method keys84* @return {Array} an array of seen keys85*/86keys: function () { throw new Error("keys: must be overridden"); },87/**88* returns true if the key is one for which a `get()` call would work.89* @method hasKey90* @param {String} key91* @return true if the key is valid for this store, false otherwise92*/93hasKey: function (/* key */) { throw new Error("hasKey: must be overridden"); },94/**95* lifecycle method to dispose temporary resources associated with the store96* @method dispose97*/98dispose: function () {},99/**100* sugar method to return an object associated with a specific key. Throws101* if the content set against the key was not a valid JSON string.102* @method getObject103* @param {String} key the key for which to return the associated object104* @return {Object} the object corresponding to the key105*/106getObject: function (key) {107return JSON.parse(this.get(key));108},109/**110* sugar method to set an object against a specific key.111* @method setObject112* @param {String} key the key for the object113* @param {Object} object the object to be stored114*/115setObject: function (key, object) {116return this.set(key, JSON.stringify(object));117}118};119120module.exports = Store;121122123124125