2015-10-28 10:37:17 -07:00
|
|
|
/* Copyright 2015 Realm Inc - All Rights Reserved
|
|
|
|
* Proprietary and Confidential
|
|
|
|
*/
|
|
|
|
|
2015-10-01 22:56:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-21 13:25:12 -07:00
|
|
|
const constants = require('./constants');
|
|
|
|
const util = require('./util');
|
2015-10-01 22:56:47 -07:00
|
|
|
|
2015-10-21 13:25:12 -07:00
|
|
|
const {keys} = constants;
|
|
|
|
const registeredConstructors = {};
|
2015-10-01 22:56:47 -07:00
|
|
|
|
2015-10-19 12:06:47 -07:00
|
|
|
module.exports = {
|
|
|
|
create,
|
|
|
|
registerConstructors,
|
|
|
|
};
|
2015-10-01 22:56:47 -07:00
|
|
|
|
|
|
|
function create(realmId, info) {
|
|
|
|
let schema = info.schema;
|
2015-10-06 00:52:15 -07:00
|
|
|
let constructor = (registeredConstructors[realmId] || {})[schema.name];
|
2015-10-01 22:56:47 -07:00
|
|
|
let object = constructor ? Object.create(constructor.prototype) : {};
|
|
|
|
let props = {};
|
|
|
|
|
2015-10-19 12:06:47 -07:00
|
|
|
object[keys.realm] = realmId;
|
|
|
|
object[keys.id] = info.id;
|
|
|
|
object[keys.type] = info.type;
|
2015-10-01 22:56:47 -07:00
|
|
|
|
2015-11-05 16:10:52 -08:00
|
|
|
schema.properties.forEach((prop) => {
|
2015-10-01 22:56:47 -07:00
|
|
|
let name = prop.name;
|
|
|
|
|
|
|
|
props[name] = {
|
2015-10-19 15:26:42 -07:00
|
|
|
get: util.getterForProperty(name),
|
|
|
|
set: util.setterForProperty(name),
|
2015-10-01 22:56:47 -07:00
|
|
|
};
|
2015-11-05 16:10:52 -08:00
|
|
|
});
|
2015-10-01 22:56:47 -07:00
|
|
|
|
|
|
|
Object.defineProperties(object, props);
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2015-10-06 00:52:15 -07:00
|
|
|
function registerConstructors(realmId, constructors) {
|
|
|
|
registeredConstructors[realmId] = constructors;
|
2015-10-01 22:56:47 -07:00
|
|
|
}
|