Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80679 views
1
/*
2
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
3
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
4
*/
5
6
var Factory = require('../util/factory'),
7
factory = new Factory('store', __dirname, false);
8
/**
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.
12
*
13
* Usage
14
* -----
15
*
16
* var Store = require('istanbul').Store,
17
* store = Store.create('memory');
18
*
19
* //basic use
20
* store.set('foo', 'foo-content');
21
* var content = store.get('foo');
22
*
23
* //keys and values
24
* store.keys().forEach(function (key) {
25
* console.log(key + ':\n' + store.get(key);
26
* });
27
* if (store.hasKey('bar') { console.log(store.get('bar'); }
28
*
29
*
30
* //syntactic sugar
31
* store.setObject('foo', { foo: true });
32
* console.log(store.getObject('foo').foo);
33
*
34
* store.dispose();
35
*
36
* @class Store
37
* @constructor
38
* @module store
39
* @param {Object} options Optional. The options supported by a specific store implementation.
40
* @main store
41
*/
42
function Store(/* options */) {}
43
44
//add register, create, mix, loadAll, getStoreList as class methods
45
factory.bindClassMethods(Store);
46
47
/**
48
* registers a new store implementation.
49
* @method register
50
* @static
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()`
53
*/
54
/**
55
* returns a store implementation of the specified type.
56
* @method create
57
* @static
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
61
*/
62
63
Store.prototype = {
64
/**
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.
68
*
69
* @method set
70
* @param {String} key the key for the content
71
* @param {String} contents the contents for the key
72
*/
73
set: function (/* key, contents */) { throw new Error("set: must be overridden"); },
74
/**
75
* returns the content associated to a specific key or throws if the key
76
* was not `set`
77
* @method get
78
* @param {String} key the key for which to get the content
79
* @return {String} the content for the specified key
80
*/
81
get: function (/* key */) { throw new Error("get: must be overridden"); },
82
/**
83
* returns a list of all known keys
84
* @method keys
85
* @return {Array} an array of seen keys
86
*/
87
keys: function () { throw new Error("keys: must be overridden"); },
88
/**
89
* returns true if the key is one for which a `get()` call would work.
90
* @method hasKey
91
* @param {String} key
92
* @return true if the key is valid for this store, false otherwise
93
*/
94
hasKey: function (/* key */) { throw new Error("hasKey: must be overridden"); },
95
/**
96
* lifecycle method to dispose temporary resources associated with the store
97
* @method dispose
98
*/
99
dispose: function () {},
100
/**
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.
103
* @method getObject
104
* @param {String} key the key for which to return the associated object
105
* @return {Object} the object corresponding to the key
106
*/
107
getObject: function (key) {
108
return JSON.parse(this.get(key));
109
},
110
/**
111
* sugar method to set an object against a specific key.
112
* @method setObject
113
* @param {String} key the key for the object
114
* @param {Object} object the object to be stored
115
*/
116
setObject: function (key, object) {
117
return this.set(key, JSON.stringify(object));
118
}
119
};
120
121
module.exports = Store;
122
123
124
125