Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions dist/ecore.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,17 @@ Ecore.EObjectPrototype = {
for (attr in attrs) {
val = attrs[attr];
if (typeof val !== 'undefined' && this.has(attr)) {

if (this.isSet(attr)) this.unset(attr);

var feature = getEStructuralFeature(this.eClass, attr),
isContainment = Boolean(feature.get('containment')) === true;
this.values[attr] = val;

if (isContainment) {
val.eContainingFeature = feature;
val.eContainer = this;
}
eve = 'change:' + attr;
this.trigger('change ' + eve, attr);
if (eResource) eResource.trigger('change', this);
Expand All @@ -398,6 +408,53 @@ Ecore.EObjectPrototype = {
return this;
},

// Unset for the property identified by the first parameter.
//
// @method unset
// @param {String} name
// @return {EObject}
unset: function(attrs, options) {

var attr, key, eve;
if (attrs === null) return this;

if (attrs.eClass) {
attrs = attrs.get('name');
}

// Handle attrs is a hash or attrs is
// property and options the value to be set.
if (!_.isObject(attrs)) {
key = attrs;
(attrs = {})[key] = undefined;
}

var eResource = this.eResource();

for (attr in attrs) {
if (this.has(attr) && this.isSet(attr)) {
// unset

var feature = getEStructuralFeature(this.eClass, attr),
isContainment = Boolean(feature.get('containment')) === true;
var value = this.values[attr];

if (isContainment) {
value.eContainingFeature = undefined;
value.eContainer = undefined;
}
this.values[attr] = undefined;
eve = 'unset:' + attr;
this.trigger('unset ' + eve, attr);
if (eResource) eResource.trigger('change', this);
}
}

return this;
},



// Getter for the property identified by the first parameter.
//
// @method get
Expand Down
2 changes: 1 addition & 1 deletion dist/ecore.min.js

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions dist/ecore.xmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,17 @@ Ecore.EObjectPrototype = {
for (attr in attrs) {
val = attrs[attr];
if (typeof val !== 'undefined' && this.has(attr)) {

if (this.isSet(attr)) this.unset(attr);

var feature = getEStructuralFeature(this.eClass, attr),
isContainment = Boolean(feature.get('containment')) === true;
this.values[attr] = val;

if (isContainment) {
val.eContainingFeature = feature;
val.eContainer = this;
}
eve = 'change:' + attr;
this.trigger('change ' + eve, attr);
if (eResource) eResource.trigger('change', this);
Expand All @@ -398,6 +408,53 @@ Ecore.EObjectPrototype = {
return this;
},

// Unset for the property identified by the first parameter.
//
// @method unset
// @param {String} name
// @return {EObject}
unset: function(attrs, options) {

var attr, key, eve;
if (attrs === null) return this;

if (attrs.eClass) {
attrs = attrs.get('name');
}

// Handle attrs is a hash or attrs is
// property and options the value to be set.
if (!_.isObject(attrs)) {
key = attrs;
(attrs = {})[key] = undefined;
}

var eResource = this.eResource();

for (attr in attrs) {
if (this.has(attr) && this.isSet(attr)) {
// unset

var feature = getEStructuralFeature(this.eClass, attr),
isContainment = Boolean(feature.get('containment')) === true;
var value = this.values[attr];

if (isContainment) {
value.eContainingFeature = undefined;
value.eContainer = undefined;
}
this.values[attr] = undefined;
eve = 'unset:' + attr;
this.trigger('unset ' + eve, attr);
if (eResource) eResource.trigger('change', this);
}
}

return this;
},



// Getter for the property identified by the first parameter.
//
// @method get
Expand Down
3 changes: 2 additions & 1 deletion dist/ecore.xmi.min.js

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions src/ecore.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,17 @@ Ecore.EObjectPrototype = {
for (attr in attrs) {
val = attrs[attr];
if (typeof val !== 'undefined' && this.has(attr)) {

if (this.isSet(attr)) this.unset(attr);

var feature = getEStructuralFeature(this.eClass, attr),
isContainment = Boolean(feature.get('containment')) === true;
this.values[attr] = val;

if (isContainment) {
val.eContainingFeature = feature;
val.eContainer = this;
}
eve = 'change:' + attr;
this.trigger('change ' + eve, attr);
if (eResource) eResource.trigger('change', this);
Expand All @@ -379,6 +389,53 @@ Ecore.EObjectPrototype = {
return this;
},

// Unset for the property identified by the first parameter.
//
// @method unset
// @param {String} name
// @return {EObject}
unset: function(attrs, options) {

var attr, key, eve;
if (attrs === null) return this;

if (attrs.eClass) {
attrs = attrs.get('name');
}

// Handle attrs is a hash or attrs is
// property and options the value to be set.
if (!_.isObject(attrs)) {
key = attrs;
(attrs = {})[key] = undefined;
}

var eResource = this.eResource();

for (attr in attrs) {
if (this.has(attr) && this.isSet(attr)) {
// unset

var feature = getEStructuralFeature(this.eClass, attr),
isContainment = Boolean(feature.get('containment')) === true;
var value = this.values[attr];

if (isContainment) {
value.eContainingFeature = undefined;
value.eContainer = undefined;
}
this.values[attr] = undefined;
eve = 'unset:' + attr;
this.trigger('unset ' + eve, attr);
if (eResource) eResource.trigger('change', this);
}
}

return this;
},



// Getter for the property identified by the first parameter.
//
// @method get
Expand Down
78 changes: 78 additions & 0 deletions test/xmi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,84 @@ describe('#XMI', function() {

});

describe('Containment feature with upper bound equal to 1:', function() {

var resourceSet,
resource,
A,
B;

before(function() {

resourceSet = Ecore.ResourceSet.create();

var P = Ecore.EPackage.create({
name: 'sample',
nsPrefix: 'sample',
nsURI: 'http://www.example.org/sample'
});

A = Ecore.EClass.create({name: 'A' });
B = Ecore.EClass.create({ name: 'B' });

var A_B = Ecore.EReference.create({
name: 'b',
upperBound: 1,
containment: true,
eType: function() { return B; }
});

A.get('eStructuralFeatures').add(A_B);

P.get('eClassifiers')
.add(A)
.add(B);

resource = resourceSet.create('model');
resource.add(P);

});

it('should serialize test3 correctly', function() {

var r = resourceSet.create({uri:'test3.xmi'}),
a = A.create({}),
b = B.create({});

a.set('b', b);

r.get('contents').add(a);

var expected = '<?xml version="1.0" encoding="UTF-8"?>' +
'\n' +
'<sample:A xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sample="http://www.example.org/sample">' +
'<b/>' +
'</sample:A>';

assert.strictEqual(r.to(Ecore.XMI), expected);

});

it('should unset correctly b1', function() {

var r = resourceSet.create({uri:'test4.xmi'}),
a = A.create({}),
b1 = B.create({}),
b2 = B.create({});

a.set('b', b1);
a.set('b', b2);

assert.ok(b1.eContainer === undefined,
"eContainer of b1 should be undefined"
);
assert.ok(b1.eContainingFeature === undefined,
"eContainingFeature of b1 should be undefined"
);
});

});

});