forked from Coffa/factory_girl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
80 lines (65 loc) · 1.91 KB
/
data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
;(function(libAPI) {
'use strict';
var container = {};
var sequences = {};
libAPI.datum = new Data();
function Data() {}
Data.prototype.checkDefined = function(name) {
if (container[name]) {
return true;
} else {
throw Error(name + ' is not defined');
}
};
Data.prototype.setDefined = function(name, opts, defined) {
this.setAlias(opts, defined);
container[name] = {factories: [], options: opts, defined: defined};
};
Data.prototype.setAlias = function(opts, defined) {
var alias = opts.alias || [];
delete opts.alias;
if (alias instanceof Array) {
for (var i = alias.length - 1; i >= 0; i--) {
this.setDefined(alias[i], opts, defined);
}
} else {
this.setDefined(alias, opts, defined);
}
};
Data.prototype.getOptions = function(name) {
this.checkDefined(name);
return container[name]['options'];
};
Data.prototype.getDefined = function(name) {
this.checkDefined(name);
return container[name]['defined'];
};
Data.prototype.createFactory = function(name, attrs) {
var factory = new libAPI.Model(name, attrs);
container[name]['factories'].push(factory);
return factory;
};
Data.prototype.remove = function(name) {
this.checkDefined(name);
delete container[name];
return this;
};
Data.prototype.clear = function() {
for(var prop in container) {
if (container.hasOwnProperty(prop)) {
this.remove(prop);
}
}
return this;
};
Data.prototype.count = function() {
return Object.keys(container).length;
};
Data.prototype.setSequence = function(name, callback) {
sequences[name] = {constructor: callback, next_id: 0};
};
Data.prototype.nextSequence = function(name) {
sequences[name]['next_id'] += 1;
return sequences[name]['constructor'](sequences[name]['next_id']);
};
})(libAPI = (typeof libAPI === 'undefined' ? {} : libAPI));