Skip to content

Commit

Permalink
incorporating stef feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed Aug 4, 2015
1 parent c64b316 commit 09218cf
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions packages/ember-metal/lib/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ let members = {
};

let memberNames = Object.keys(members);
let memberProperties = memberNames.map(memberProperty);

function Meta(obj, parentMeta) {
// preallocate a slot for each member
for (let i = 0; i < memberNames.length; i++) {
this[memberProperty(memberNames[i])] = undefined;
}
memberProperties.forEach(prop => this[prop] = undefined);

// used only internally
this.source = obj;
Expand All @@ -69,34 +68,26 @@ function Meta(obj, parentMeta) {
for (let name in listenerMethods) {
Meta.prototype[name] = listenerMethods[name];
}

(function setupMembers() {
for (let i = 0; i < memberNames.length; i++) {
let name = memberNames[i];
let implementation = members[name];
implementation(name, Meta);
}
})();

memberNames.forEach(name => members[name](name, Meta));

// Implements a member that is a lazily created, non-inheritable
// POJO.
function ownMap(name, Meta) {
let key = memberProperty(name);
let capitalized = capitalize(name);
Meta.prototype['writable' + capitalized] = function() {
return getOrCreateOwnMap.call(this, key);
return this._getOrCreateOwnMap(key);
};
Meta.prototype['readable' + capitalized] = function() { return this[key]; };
}

function getOrCreateOwnMap(key) {
Meta.prototype._getOrCreateOwnMap = function(key) {
let ret = this[key];
if (!ret) {
ret = this[key] = {};
}
return ret;
}
};

// Implements a member that is a lazily created POJO with inheritable
// values.
Expand All @@ -105,15 +96,15 @@ function inheritedMap(name, Meta) {
let capitalized = capitalize(name);

Meta.prototype['writable' + capitalized] = function() {
return getOrCreateInheritedMap.call(this, key);
return this._getOrCreateInheritedMap(key);
};

Meta.prototype['readable' + capitalized] = function() {
return getInherited.call(this, key);
return this._getInherited(key);
};

Meta.prototype['peek' + capitalized] = function(subkey) {
let map = getInherited.call(this, key);
let map = this._getInherited(key);
if (map) {
return map[subkey];
}
Expand All @@ -124,27 +115,27 @@ function inheritedMap(name, Meta) {
};
}

function getOrCreateInheritedMap(key) {
Meta.prototype._getOrCreateInheritedMap = function(key) {
let ret = this[key];
if (!ret) {
if (this.parent) {
ret = this[key] = Object.create(getOrCreateInheritedMap.call(this.parent, key));
ret = this[key] = Object.create(this.parent._getOrCreateInheritedMap(key));
} else {
ret = this[key] = {};
}
}
return ret;
}
};

function getInherited(key) {
Meta.prototype._getInherited = function(key) {
let pointer = this;
while (pointer) {
while (pointer !== undefined) {
if (pointer[key]) {
return pointer[key];
}
pointer = pointer.parent;
}
}
};

// Implements a member that provides a lazily created map of maps,
// with inheritance at both levels.
Expand All @@ -153,7 +144,7 @@ function inheritedMapOfMaps(name, Meta) {
let capitalized = capitalize(name);

Meta.prototype['writable' + capitalized] = function(subkey) {
let outerMap = getOrCreateInheritedMap.call(this, key);
let outerMap = this._getOrCreateInheritedMap(key);
let innerMap = outerMap[subkey];
if (!innerMap) {
innerMap = outerMap[subkey] = {};
Expand All @@ -164,14 +155,14 @@ function inheritedMapOfMaps(name, Meta) {
};

Meta.prototype['readable' + capitalized] = function(subkey) {
let map = getInherited.call(this, key);
let map = this._getInherited(key);
if (map) {
return map[subkey];
}
};

Meta.prototype['getAll' + capitalized] = function() {
return getInherited.call(this, key);
return this._getInherited(key);
};
}

Expand All @@ -198,19 +189,19 @@ function ownCustomObject(name, Meta) {
function inheritedCustomObject(name, Meta) {
let key = memberProperty(name);
let capitalized = capitalize(name);
let writable = Meta.prototype['writable' + capitalized] = function(create) {
Meta.prototype['writable' + capitalized] = function(create) {
let ret = this[key];
if (!ret) {
if (this.parent) {
ret = this[key] = writable.call(this.parent, create).copy(this.source);
ret = this[key] = this.parent['writable' + capitalized](create).copy(this.source);
} else {
ret = this[key] = create(this.source);
}
}
return ret;
};
Meta.prototype['readable' + capitalized] = function() {
return getInherited.call(this, key);
return this._getInherited(key);
};
}

Expand Down

0 comments on commit 09218cf

Please sign in to comment.