From 830209ad2ab2144992895fbd175cc701484c8036 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Sat, 5 Apr 2014 17:17:10 +0100 Subject: [PATCH] move trait addition to private function inside TraitBuilder --- index.js | 6 +----- lib/TraitBuilder.js | 23 +++++++---------------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index 64da8a2..9855281 100644 --- a/index.js +++ b/index.js @@ -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 || {}); diff --git a/lib/TraitBuilder.js b/lib/TraitBuilder.js index 00e0370..115baf7 100644 --- a/lib/TraitBuilder.js +++ b/lib/TraitBuilder.js @@ -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); } @@ -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; @@ -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); }