all files / montage/core/serialization/deserializer/ self-deserializer.js

81.08% Statements 30/37
50% Branches 10/20
77.78% Functions 7/9
81.08% Lines 30/37
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                               133× 133× 133× 133×   133×           1876× 1876×                                                                                                                  
var Montage = require("../../core").Montage;
 
var SelfDeserializer = Montage.specialize( {
    _object: {value: null},
    _objectDescriptor: {value: null},
    _context: {value: null},
    _unitNames: {value: null},
    _objectUnitNames: {value: null},
 
    create: {
        value: function () {
            return new this();
        }
    },
 
    initWithObjectAndObjectDescriptorAndContextAndUnitNames: {
        value: function (object, objectDescriptor, context, unitNames) {
            this._object = object;
            this._objectDescriptor = objectDescriptor;
            this._context = context;
            this._unitNames = unitNames;
 
            return this;
        }
    },
 
    getProperty: {
        value: function (name) {
            Eif (this._objectDescriptor.properties) {
                return this._objectDescriptor.properties[name];
            }
        }
    },
 
    getType: {
        value: function () {
            if ("prototype" in this._objectDescriptor) {
                return "prototype";
            } else Eif ("object" in this._objectDescriptor) {
                return "object";
            }
        }
    },
 
    getTypeValue: {
        value: function () {
            return this._objectDescriptor.prototype || this._objectDescriptor.object;
        }
    },
 
    getObjectByLabel: {
        value: function (label) {
            this._context.getObject(label);
        }
    },
 
    deserializeProperties: {
        value: function (propertyNames) {
            var object = this._object,
                properties = this._objectDescriptor.properties,
                propertyName;
 
            Eif (properties) {
                Eif (!propertyNames) {
                    propertyNames = Montage.getSerializablePropertyNames(object);
                }
 
                for (var i = 0, ii = propertyNames.length; i < ii; i++) {
                    propertyName = propertyNames[i];
                    object[propertyName] = properties[propertyName];
                }
            }
        }
    },
 
    deserializeUnit: {
        value: function (name) {
            var objectUnitNames = this._objectUnitNames;
 
            Eif (!objectUnitNames) {
                objectUnitNames = this._objectUnitNames = [name];
                this._context.setUnitsToDeserialize(this._object, this._objectDescriptor, objectUnitNames);
            } else if (objectUnitNames.indexOf(name) === -1) {
                objectUnitNames.push(name);
            }
        }
    },
 
    deserializeUnits: {
        value: function () {
            var objectUnitNames = this._objectUnitNames;
 
            Eif (!objectUnitNames) {
                objectUnitNames = this._objectUnitNames = this._unitNames;
                this._context.setUnitsToDeserialize(this._object, this._objectDescriptor, objectUnitNames);
            } else {
                for (var i = 0, name; name = objectUnitNames[i]; i++) {
                    if (objectUnitNames.indexOf(name) === -1) {
                        objectUnitNames.push(name);
                    }
                }
            }
        }
    }
});
 
exports.SelfDeserializer = SelfDeserializer;