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 |
|
/*jslint nomen: true */ |
7 |
1 |
var Factory = require('../util/factory'), |
8 |
|
factory = new Factory('store', __dirname, false); |
9 |
|
/** |
10 |
|
* An abstraction for keeping track of content against some keys (e.g. |
11 |
|
* original source, instrumented source, coverage objects against file names). |
12 |
|
* This class is both the base class as well as a factory for `Store` implementations. |
13 |
|
* |
14 |
|
* Usage |
15 |
|
* ----- |
16 |
|
* |
17 |
|
* var Store = require('istanbul').Store, |
18 |
|
* store = Store.create('memory'); |
19 |
|
* |
20 |
|
* //basic use |
21 |
|
* store.set('foo', 'foo-content'); |
22 |
|
* var content = store.get('foo'); |
23 |
|
* |
24 |
|
* //keys and values |
25 |
|
* store.keys().forEach(function (key) { |
26 |
|
* console.log(key + ':\n' + store.get(key); |
27 |
|
* }); |
28 |
|
* if (store.hasKey('bar') { console.log(store.get('bar'); } |
29 |
|
* |
30 |
|
* |
31 |
|
* //syntactic sugar |
32 |
|
* store.setObject('foo', { foo: true }); |
33 |
|
* console.log(store.getObject('foo').foo); |
34 |
|
* |
35 |
|
* store.dispose(); |
36 |
|
* |
37 |
|
* @class Store |
38 |
|
* @constructor |
39 |
|
* @protected |
40 |
|
* @param {Object} options Optional. The options supported by a specific store implementation. |
41 |
|
*/ |
42 |
1 |
function Store(options) {} |
43 |
|
|
44 |
|
//add register, create, mix, loadAll, getStoreList as class methods |
45 |
1 |
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 |
1 |
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 |
1 |
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 |
1 |
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 |
1 |
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 |
1 |
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 |
57 |
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 |
29 |
return this.set(key, JSON.stringify(object)); |
118 |
|
} |
119 |
|
}; |
120 |
|
|
121 |
1 |
module.exports = Store; |
122 |
|
|
123 |
|
|
124 |
|
|