MCPcopy Create free account
hub / github.com/culvertsoft/mgen / createClass

Function createClass

mgen-javascriptlib/src/main/javascript/mgen-lib.js:232–308  ·  view source on GitHub ↗
(obj)

Source from the content-addressed store, hash-verified

230 }
231
232 function createClass(obj) {
233 var _c = {}; // the _c object is used to trick the function.name property to be correct for the class.
234 // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
235 _c[tmpWrap(obj.__t)] = function(data, options) {
236 var field, default_construction = false;
237
238 if (data === "NO_CONSTRUCT") {
239 this.__t = obj.__t;
240 return;
241 }
242
243 if (data === "DEFAULT") {
244 default_construction = true;
245 data = null;
246 }
247
248 /* Options are local settings that inherits settings from the ClassRegistry */
249 options = options || {};
250 extend(options, settings);
251
252 if (data) {
253 if (data.__t) {
254 //If the data does not fit the corresponding type then check if it's a derived type.
255 if (obj.__t != data.__t) {
256 if (isDerivedType(data.__t, obj.__t)) {
257 return new(ClassRegistry.getClass(tmpWrap(data.__t)))(data);
258 } else {
259 throw Error(" Tried to create " + tmpWrap(obj.__t) + "("+obj.__t+") but got " + tmpWrap(data.__t) + " ("+data.__t+")");
260 }
261 }
262 }
263 if (options.validate) {
264 validateIndata(obj, data);
265 }
266 }
267 if(data === undefined || data === null) {
268 data = {};
269 }
270
271 // set default fields for fields that have default values and no data.
272 for (field in obj){
273 if (!obj.hasOwnProperty(field)) continue;
274 if( obj[field]["default"] !== undefined ) {
275 data[field] = obj[field]["default"];
276 }
277 }
278
279 //populate fields with data.
280 for (field in obj) {
281 if (!obj.hasOwnProperty(field)) continue;
282
283 if (field === "__t") {
284 this.__t = obj.__t;
285 } else {
286 try {
287 if (options.validate && !default_construction) {
288 this[field] = createField(obj[field], field, data, options);
289 } else if (default_construction) {

Callers 1

registerClassFunction · 0.85

Calls 7

tmpWrapFunction · 0.85
extendFunction · 0.85
isDerivedTypeFunction · 0.85
validateIndataFunction · 0.85
createFieldFunction · 0.85
createDefaultFieldFunction · 0.85
getClassMethod · 0.80

Tested by

no test coverage detected