Skip to content

Commit

Permalink
move trait addition to private function inside TraitBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
jaz303 committed Apr 5, 2014
1 parent 37f970c commit 830209a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
6 changes: 1 addition & 5 deletions index.js
Expand Up @@ -8,13 +8,9 @@ exports.extend = extend;

function make(traits, opts) {

var builder = new TraitBuilder();

traits = registry.expand(traits);

traits.forEach(function(t) {
builder.require(t);
});
var builder = new TraitBuilder(traits);

var ctor = builder.__compile__(opts || {});

Expand Down
23 changes: 7 additions & 16 deletions lib/TraitBuilder.js
Expand Up @@ -14,13 +14,15 @@ function makeChain(fns) {
}
}

function TraitBuilder() {
function TraitBuilder(traits) {

this._traits = [];
this._namedTraits = {};

this._chains = {};
this._properties = {};

traits.forEach(function(t) { add(this, t); }, this);

}

Expand Down Expand Up @@ -66,18 +68,7 @@ TraitBuilder.prototype.__compile__ = function(opts) {

}

/*
* Add a trait to this definition.
*
* This method is a no-op if traitDesc has already been added.
*
* @param traitDesc trait descriptor, either:
* - string (trait name)
* - function (anonymous trait)
* - [{string|function}, args...] (trait with arguments)
* - object (equivalent to [object.trait, object])
*/
TraitBuilder.prototype.require = function(traitDesc) {
function add(self, traitDesc) {

var trait, args, resolved;

Expand All @@ -99,17 +90,17 @@ TraitBuilder.prototype.require = function(traitDesc) {
var resolved = registry.get(trait);

// don't add duplicate traits
if (this._traits.indexOf(resolved) >= 0) {
if (self._traits.indexOf(resolved) >= 0) {
return;
}

var instance = new TraitInstance(resolved, args);

if (typeof trait === 'string') {
this._namedTraits[trait] = instance;
self._namedTraits[trait] = instance;
}

this._traits.push(instance);
self._traits.push(instance);

}

Expand Down

0 comments on commit 830209a

Please sign in to comment.