UNPKG

3.48 kBJavaScriptView Raw
1'use strict';
2var MasterDataResource, MemoryResource;
3
4MemoryResource = require('./memory-resource');
5
6
7/**
8
9@class MasterDataResource
10@implements ResourceClientInterface
11 */
12
13MasterDataResource = (function() {
14
15 /**
16 load master JSON file if exists
17
18 @constructor
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 Get master data dir
32
33 @method getDirPath
34 @public
35 @static
36 @return {String}
37 */
38
39 MasterDataResource.getDirPath = function(dirname) {
40 return dirname + '/master-data';
41 };
42
43
44 /**
45 Get master JSON path
46
47 @method getJSONPath
48 @public
49 @static
50 @return {String}
51 */
52
53 MasterDataResource.getJSONPath = function(dirname) {
54 return this.getDirPath(dirname) + '/all.json';
55 };
56
57
58 /**
59 load data from directory(Node.js) or JSON (other environments)
60
61 @method init
62 @public
63 @chainable
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 load data from JSON file
92
93 @method loadFromJSON
94 @private
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 Get memory resource of the given modelName
111 @method getMemoryResource
112 @return {MemoryResource}
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 Create JSON file from tsv files (**only called by Node.js**)
123
124 @method build
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 Create plain object
138
139 @method toPlainObject
140 @return {Object} plainObject
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
158module.exports = MasterDataResource;