Skip to content

Commit

Permalink
Merge pull request #1324 from aadsm/template-properties-serialization
Browse files Browse the repository at this point in the history
Template properties serialization
  • Loading branch information
asolove committed Oct 24, 2013
2 parents 27fc36d + 725953b commit a8f24fb
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
24 changes: 24 additions & 0 deletions core/serialization/serializer/montage-labeler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@ exports.MontageLabeler = Montage.specialize.call(Labeler, {
}
},

getTemplatePropertyLabel: {
value: function(object) {
var label = this.superForValue("getObjectLabel")(object);

if (label[0] !== ":") {
throw new Error("Template property's labels need to start with a colon (:), (\"" + label + "\").");
}

return label;
}
},

getObjectLabel: {
value: function(object) {
var label = this.super(object);

if (label[0] === ":") {
throw new Error("Labels starting with colon (:) can only be used for template properties, (\"" + label + "\").");
}

return label;
}
},

getObjectName: {
value: function(object) {
var identifier = object.identifier,
Expand Down
17 changes: 16 additions & 1 deletion core/serialization/serializer/montage-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var MontageSerializerModule = require("./montage-serializer");
var PropertiesSerializer = require("./properties-serializer").PropertiesSerializer;
var SelfSerializer = require("./self-serializer").SelfSerializer;
var UnitSerializer = require("./unit-serializer").UnitSerializer;
var Alias = require("core/serialization/alias").Alias;
var Visitor = require("mousse/serialization/visitor").Visitor;

var MontageVisitor = Montage.specialize.call(Visitor, {
Expand All @@ -29,9 +30,12 @@ var MontageVisitor = Montage.specialize.call(Visitor, {

getTypeOf: {
value: function(object) {
// Module and Alias are MontageObject's too so they need to be
// tested for before.
if (object.isModuleReference) {
// this needs to be first as a ModuleReference is also a MontageObject
return "Module";
} else if (object instanceof Alias) {
return "Alias";
} else if ("getInfoForObject" in object || "getInfoForObject" in object.constructor) {
return "MontageObject";
} else if (object.thisIsAReferenceCreatedByMontageSerializer) {
Expand Down Expand Up @@ -84,6 +88,17 @@ var MontageVisitor = Montage.specialize.call(Visitor, {
}
},

visitAlias: {
value: function(malker, object) {
var label = this.labeler.getTemplatePropertyLabel(object);

var builderObject = this.builder.createCustomObject();

builderObject.setProperty("alias", object.value);
this.builder.top.setProperty(label, builderObject);
}
},

visitMontageObject: {
value: function(malker, object, name) {
if (this.isObjectSerialized(object)) {
Expand Down
73 changes: 72 additions & 1 deletion test/serialization/montage-serializer-spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var Montage = require("montage/core/core").Montage,
MontageSerializer = require("montage/core/serialization").Serializer,
objects = require("serialization/testobjects-v2").objects,
ModuleReference = require("montage/core/module-reference").ModuleReference;
ModuleReference = require("montage/core/module-reference").ModuleReference,
Alias = require("montage/core/serialization/alias").Alias;

function fakeGetSerializablePropertyNames(object, returnValues) {
getSerializablePropertyNames = Montage.getSerializablePropertyNames;
Expand Down Expand Up @@ -1943,4 +1944,74 @@ describe("serialization/montage-serializer-spec", function() {

});
});

describe("Template properties serialization", function() {
it("should serialize a template property alias", function() {
var object = {
":templateProperty": new Alias().init("@component:propertyName")
},
expectedSerialization,
serialization;

expectedSerialization = {
":templateProperty": {
"alias": "@component:propertyName"
}
};

serialization = serializer.serialize(object);
expect(JSON.parse(serialization))
.toEqual(expectedSerialization);
});

it("should not serialize a alias outside a template property", function() {
var object = {
"property": new Alias().init("@component:propertyName")
};

expect(function() {
serializer.serialize(object);
}).toThrow();
});

it("should not serialize a value with a template property label", function() {
var object = {
":property": 42
};

expect(function() {
serializer.serialize(object);
}).toThrow();
});

it("should not serialize an object literal with a template property label", function() {
var object = {
":property": {}
};

expect(function() {
serializer.serialize(object);
}).toThrow();
});

it("should not serialize a regexp with a template property label", function() {
var object = {
":property": /regexp/
};

expect(function() {
serializer.serialize(object);
}).toThrow();
});

it("should not serialize a montage object with a template property label", function() {
var object = {
":property": objects.Empty
};

expect(function() {
serializer.serialize(object);
}).toThrow();
});
});
});

0 comments on commit a8f24fb

Please sign in to comment.