1 | 'use strict';
|
2 | var MasterDataResource, MemoryResource;
|
3 |
|
4 | MemoryResource = require('./memory-resource');
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | MasterDataResource = (function() {
|
14 |
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 | function MasterDataResource(facade) {
|
21 | var dirname;
|
22 | this.facade = facade;
|
23 | dirname = this.facade.dirname;
|
24 | this.masterDirPath = this.constructor.getDirPath(dirname);
|
25 | this.masterJSONPath = this.constructor.getJSONPath(dirname);
|
26 | this.memories = {};
|
27 | }
|
28 |
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | MasterDataResource.getDirPath = function(dirname) {
|
40 | return dirname + '/master-data';
|
41 | };
|
42 |
|
43 |
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | MasterDataResource.getJSONPath = function(dirname) {
|
54 | return this.getDirPath(dirname) + '/all.json';
|
55 | };
|
56 |
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | MasterDataResource.prototype.init = function() {
|
67 | var modelName, plainMemories, plainMemory;
|
68 | if ((typeof Ti === "undefined" || Ti === null) && (typeof window === "undefined" || window === null)) {
|
69 | this.build();
|
70 | } else {
|
71 | plainMemories = this.loadFromJSON();
|
72 | for (modelName in plainMemories) {
|
73 | plainMemory = plainMemories[modelName];
|
74 | this.memories[modelName] = MemoryResource.restore(plainMemory);
|
75 | }
|
76 | }
|
77 | return this;
|
78 | };
|
79 |
|
80 | MasterDataResource.prototype.initWithData = function(data) {
|
81 | var modelName, plainMemory;
|
82 | for (modelName in data) {
|
83 | plainMemory = data[modelName];
|
84 | this.memories[modelName] = MemoryResource.restore(plainMemory);
|
85 | }
|
86 | return this;
|
87 | };
|
88 |
|
89 |
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | MasterDataResource.prototype.loadFromJSON = function() {
|
98 | var e, error, requireJSON;
|
99 | requireJSON = this.facade.constructor.requireJSON;
|
100 | try {
|
101 | return requireJSON(this.masterJSONPath);
|
102 | } catch (error) {
|
103 | e = error;
|
104 | return console.error("base-domain: [warning] MasterDataResource could not load from path '" + this.masterJSONPath + "'");
|
105 | }
|
106 | };
|
107 |
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | MasterDataResource.prototype.getMemoryResource = function(modelName) {
|
116 | var base;
|
117 | return (base = this.memories)[modelName] != null ? base[modelName] : base[modelName] = new MemoryResource;
|
118 | };
|
119 |
|
120 |
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | MasterDataResource.prototype.build = function() {
|
128 | var FixtureLoader, fs;
|
129 | FixtureLoader = require('./fixture-loader');
|
130 | new FixtureLoader(this.facade, this.masterDirPath).load();
|
131 | fs = this.facade.constructor.fs;
|
132 | return fs.writeFileSync(this.masterJSONPath, JSON.stringify(this.toPlainObject(), null, 1));
|
133 | };
|
134 |
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | MasterDataResource.prototype.toPlainObject = function() {
|
144 | var memory, modelName, plainObj, ref;
|
145 | plainObj = {};
|
146 | ref = this.memories;
|
147 | for (modelName in ref) {
|
148 | memory = ref[modelName];
|
149 | plainObj[modelName] = memory.toPlainObject();
|
150 | }
|
151 | return plainObj;
|
152 | };
|
153 |
|
154 | return MasterDataResource;
|
155 |
|
156 | })();
|
157 |
|
158 | module.exports = MasterDataResource;
|