dojo/store/Memory.js

Maintainability

68.70

Lines of code

164

Created with Raphaël 2.1.002550751002015-1-52014-12-42014-12-3

2015-5-18
Maintainability: 68.7

Created with Raphaël 2.1.00501001502002015-1-52014-12-42014-12-3

2015-5-18
Lines of Code: 164

Difficulty

46.52

Estimated Errors

0.65

Function weight

By Complexity

Created with Raphaël 2.1.0put5

By SLOC

Created with Raphaël 2.1.0<anonymous>163
1
define(["../_base/declare", "./util/QueryResults", "./util/SimpleQueryEngine" /*=====, "./api/Store" =====*/],
2
function(declare, QueryResults, SimpleQueryEngine /*=====, Store =====*/){
3
 
4
// module:
5
//      dojo/store/Memory
6
 
7
// No base class, but for purposes of documentation, the base class is dojo/store/api/Store
8
var base = null;
9
/*===== base = Store; =====*/
10
 
11
return declare("dojo.store.Memory", base, {
12
    // summary:
13
    //      This is a basic in-memory object store. It implements dojo/store/api/Store.
14
    constructor: function(options){
15
        // summary:
16
        //      Creates a memory object store.
17
        // options: dojo/store/Memory
18
        //      This provides any configuration information that will be mixed into the store.
19
        //      This should generally include the data property to provide the starting set of data.
20
        for(var i in options){
21
            this[i] = options[i];
22
        }
23
        this.setData(this.data || []);
24
    },
25
    // data: Array
26
    //      The array of all the objects in the memory store
27
    data:null,
28
 
29
    // idProperty: String
30
    //      Indicates the property to use as the identity property. The values of this
31
    //      property should be unique.
32
    idProperty: "id",
33
 
34
    // index: Object
35
    //      An index of data indices into the data array by id
36
    index:null,
37
 
38
    // queryEngine: Function
39
    //      Defines the query engine to use for querying the data store
40
    queryEngine: SimpleQueryEngine,
41
    get: function(id){
42
        // summary:
43
        //      Retrieves an object by its identity
44
        // id: Number
45
        //      The identity to use to lookup the object
46
        // returns: Object
47
        //      The object in the store that matches the given id.
48
        return this.data[this.index[id]];
49
    },
50
    getIdentity: function(object){
51
        // summary:
52
        //      Returns an object's identity
53
        // object: Object
54
        //      The object to get the identity from
55
        // returns: Number
56
        return object[this.idProperty];
57
    },
58
    put: function(object, options){
59
        // summary:
60
        //      Stores an object
61
        // object: Object
62
        //      The object to store.
63
        // options: dojo/store/api/Store.PutDirectives?
64
        //      Additional metadata for storing the data.  Includes an "id"
65
        //      property if a specific id is to be used.
66
        // returns: Number
67
        var data = this.data,
68
            index = this.index,
69
            idProperty = this.idProperty;
70
        var id = object[idProperty] = (options && "id" in options) ? options.id : idProperty in object ? object[idProperty] : Math.random();
71
        if(id in index){
72
            // object exists
73
            if(options && options.overwrite === false){
74
                throw new Error("Object already exists");
75
            }
76
            // replace the entry in data
77
            data[index[id]] = object;
78
        }else{
79
            // add the new object
80
            index[id] = data.push(object) - 1;
81
        }
82
        return id;
83
    },
84
    add: function(object, options){
85
        // summary:
86
        //      Creates an object, throws an error if the object already exists
87
        // object: Object
88
        //      The object to store.
89
        // options: dojo/store/api/Store.PutDirectives?
90
        //      Additional metadata for storing the data.  Includes an "id"
91
        //      property if a specific id is to be used.
92
        // returns: Number
93
        (options = options || {}).overwrite = false;
94
        // call put with overwrite being false
95
        return this.put(object, options);
96
    },
97
    remove: function(id){
98
        // summary:
99
        //      Deletes an object by its identity
100
        // id: Number
101
        //      The identity to use to delete the object
102
        // returns: Boolean
103
        //      Returns true if an object was removed, falsy (undefined) if no object matched the id
104
        var index = this.index;
105
        var data = this.data;
106
        if(id in index){
107
            data.splice(index[id], 1);
108
            // now we have to reindex
109
            this.setData(data);
110
            return true;
111
        }
112
    },
113
    query: function(query, options){
114
        // summary:
115
        //      Queries the store for objects.
116
        // query: Object
117
        //      The query to use for retrieving objects from the store.
118
        // options: dojo/store/api/Store.QueryOptions?
119
        //      The optional arguments to apply to the resultset.
120
        // returns: dojo/store/api/Store.QueryResults
121
        //      The results of the query, extended with iterative methods.
122
        //
123
        // example:
124
        //      Given the following store:
125
        //
126
        //  |   var store = new Memory({
127
        //  |       data: [
128
        //  |           {id: 1, name: "one", prime: false },
129
        //  |           {id: 2, name: "two", even: true, prime: true},
130
        //  |           {id: 3, name: "three", prime: true},
131
        //  |           {id: 4, name: "four", even: true, prime: false},
132
        //  |           {id: 5, name: "five", prime: true}
133
        //  |       ]
134
        //  |   });
135
        //
136
        //  ...find all items where "prime" is true:
137
        //
138
        //  |   var results = store.query({ prime: true });
139
        //
140
        //  ...or find all items where "even" is true:
141
        //
142
        //  |   var results = store.query({ even: true });
143
        return QueryResults(this.queryEngine(query, options)(this.data));
144
    },
145
    setData: function(data){
146
        // summary:
147
        //      Sets the given data as the source for this store, and indexes it
148
        // data: Object[]
149
        //      An array of objects to use as the source of data.
150
        if(data.items){
151
            // just for convenience with the data format IFRS expects
152
            this.idProperty = data.identifier || this.idProperty;
153
            data = this.data = data.items;
154
        }else{
155
            this.data = data;
156
        }
157
        this.index = {};
158
        for(var i = 0, l = data.length; i < l; i++){
159
            this.index[data[i][this.idProperty]] = i;
160
        }
161
    }
162
});
163
 
164
});