2 Copyright (c) 2012, Yahoo! Inc. All rights reserved.
3 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
6 var Factory = require('../util/factory'),
7 factory = new Factory('store', __dirname, false);
9 * An abstraction for keeping track of content against some keys (e.g.
10 * original source, instrumented source, coverage objects against file names).
11 * This class is both the base class as well as a factory for `Store` implementations.
16 * var Store = require('istanbul').Store,
17 * store = Store.create('memory');
20 * store.set('foo', 'foo-content');
21 * var content = store.get('foo');
24 * store.keys().forEach(function (key) {
25 * console.log(key + ':\n' + store.get(key);
27 * if (store.hasKey('bar') { console.log(store.get('bar'); }
31 * store.setObject('foo', { foo: true });
32 * console.log(store.getObject('foo').foo);
39 * @param {Object} options Optional. The options supported by a specific store implementation.
42 function Store(/* options */) {}
44 //add register, create, mix, loadAll, getStoreList as class methods
45 factory.bindClassMethods(Store);
48 * registers a new store implementation.
51 * @param {Function} constructor the constructor function for the store. This function must have a
52 * `TYPE` property of type String, that will be used in `Store.create()`
55 * returns a store implementation of the specified type.
58 * @param {String} type the type of store to create
59 * @param {Object} opts Optional. Options specific to the store implementation
60 * @return {Store} a new store of the specified type
65 * sets some content associated with a specific key. The manner in which
66 * duplicate keys are handled for multiple `set()` calls with the same
67 * key is implementation-specific.
70 * @param {String} key the key for the content
71 * @param {String} contents the contents for the key
73 set: function (/* key, contents */) { throw new Error("set: must be overridden"); },
75 * returns the content associated to a specific key or throws if the key
78 * @param {String} key the key for which to get the content
79 * @return {String} the content for the specified key
81 get: function (/* key */) { throw new Error("get: must be overridden"); },
83 * returns a list of all known keys
85 * @return {Array} an array of seen keys
87 keys: function () { throw new Error("keys: must be overridden"); },
89 * returns true if the key is one for which a `get()` call would work.
92 * @return true if the key is valid for this store, false otherwise
94 hasKey: function (/* key */) { throw new Error("hasKey: must be overridden"); },
96 * lifecycle method to dispose temporary resources associated with the store
99 dispose: function () {},
101 * sugar method to return an object associated with a specific key. Throws
102 * if the content set against the key was not a valid JSON string.
104 * @param {String} key the key for which to return the associated object
105 * @return {Object} the object corresponding to the key
107 getObject: function (key) {
108 return JSON.parse(this.get(key));
111 * sugar method to set an object against a specific key.
113 * @param {String} key the key for the object
114 * @param {Object} object the object to be stored
116 setObject: function (key, object) {
117 return this.set(key, JSON.stringify(object));
121 module.exports = Store;