Skip to content

Commit baefe5f

Browse files
committed
Talents should be applicable to Class as static methods. Closes #19.
1 parent b790288 commit baefe5f

3 files changed

Lines changed: 133 additions & 10 deletions

File tree

lib/cocktail.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ cocktail = {
2929
_processors: {},
3030

3131
/**
32-
* @protected
32+
* @protected
3333
* Returns the processor list map
3434
*/
3535
getProcessors : function(){
@@ -63,7 +63,7 @@ cocktail = {
6363
for(key in processorsConfig){
6464
if(processorsConfig.hasOwnProperty(key)){
6565
processors[key] = processorsConfig[key];
66-
}
66+
}
6767
}
6868
},
6969

@@ -108,7 +108,7 @@ cocktail = {
108108
*/
109109
_configureProcessorsWith: function(options){
110110
var key, value, processor;
111-
111+
112112
this._cleanQueue();
113113

114114
if(options){
@@ -219,7 +219,7 @@ cocktail = {
219219
}
220220
}
221221

222-
return isClassDef;
222+
return isClassDef;
223223
},
224224

225225
/**
@@ -233,7 +233,7 @@ cocktail = {
233233
/**
234234
* @private
235235
* If the subject has a property construtor returns it,
236-
* if no constructor on subject but it extends then return a function() calling super constructor,
236+
* if no constructor on subject but it extends then return a function() calling super constructor,
237237
* or a function definition otherwise.
238238
*/
239239
_getDefaultClassConstructor: function (subject) {
@@ -278,8 +278,8 @@ cocktail = {
278278

279279
defaultConstructor = this._getDefaultClassConstructor(subject);
280280
if(this._isPropertyDefinedIn('constructor', subject)) {
281-
delete subject.constructor;
282-
}
281+
delete subject.constructor;
282+
}
283283
options = subject;
284284
return this.mix(defaultConstructor, options);
285285
},
@@ -305,10 +305,10 @@ cocktail = {
305305
if (this._isModuleDefinition(subject)) {
306306
return this._processModuleDefinition(subject);
307307
}
308-
}
308+
}
309309

310310
if(subject){
311-
this._pushQueue();
311+
this._pushQueue();
312312
this._applyDefaultsOptions(options);
313313
this._configureProcessorsWith(options);
314314
this._executeProcessorsOn(subject, options);
@@ -335,7 +335,7 @@ cocktail._DEFAULT_PROCESSORS = {
335335
'@properties' : require('./processor/annotation/Properties'),
336336
'@traits' : require('./processor/annotation/Traits'),
337337
'@requires' : require('./processor/annotation/Requires'),
338-
'@talents' : require('./processor/annotation/Traits'),
338+
'@talents' : require('./processor/annotation/Talents'),
339339
'@annotation' : require('./processor/annotation/Annotation'),
340340
'@exports' : require('./processor/annotation/Exports'),
341341
'@static' : require('./processor/annotation/Static'),
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
*
3+
* Copyright (c) 2013 - 2014 Maximiliano Fierro
4+
* Licensed under the MIT license.
5+
*/
6+
'use strict';
7+
8+
9+
var Traits = require('./Traits'),
10+
Talents;
11+
12+
Talents = function(){
13+
Traits.call(this);
14+
};
15+
16+
Talents.prototype = Object.create(Traits.prototype);
17+
18+
Talents.prototype._applyTalentTo = Traits.prototype._applyTraitTo;
19+
20+
Talents.prototype.process = function(subject){
21+
var traits = this.getParameter(), // always an []
22+
l = traits.length,
23+
i;
24+
25+
for(i = 0; i < l; i++){
26+
this._applyTalentTo(subject, traits[i]);
27+
}
28+
};
29+
30+
module.exports = Talents;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
'use strict';
2+
3+
var chai = require("chai"),
4+
expect = chai.expect,
5+
cocktail = require('../../../../lib/cocktail'),
6+
// Requires = require('../../../../lib/processor/annotation/Requires.js'),
7+
Talents = require('../../../../lib/processor/annotation/Talents.js');
8+
9+
describe('Annotation Processor @talents', function(){
10+
var sut = new Talents();
11+
12+
it('has retain set false', function(){
13+
expect(sut.retain).to.equal(false);
14+
});
15+
16+
it('has priority set to cocktail.SEQUENCE.TRAITS', function(){
17+
expect(sut.priority).to.equal(cocktail.SEQUENCE.TRAITS);
18+
});
19+
20+
describe('Parameter for @talents annotation', function(){
21+
22+
it('accepts an array of Talents as parameter', function(){
23+
var sut = new Talents(),
24+
traitDef = {};
25+
26+
sut.setParameter([traitDef]);
27+
28+
expect(sut.getParameter()).to.contain(traitDef);
29+
});
30+
});
31+
32+
describe('Talents process', function(){
33+
34+
describe('Passing a Talent reference `@talents:[TalentA]`', function(){
35+
var sut = new Talents(),
36+
TalentA = function(){},
37+
myObj = {},
38+
aMethod = function method(){};
39+
40+
TalentA.prototype.aMethod = aMethod;
41+
myObj.foo = 1;
42+
43+
sut.setParameter([TalentA]);
44+
45+
sut.process(myObj);
46+
47+
it('makes the TalentA methods part of the given myObj', function(){
48+
expect(myObj).to.respondTo('aMethod');
49+
expect(myObj.aMethod).to.be.equal(aMethod);
50+
});
51+
});
52+
53+
54+
describe('Passing a Talent using options object `@talents: [{talent: TalentA}]`', function(){
55+
var sut = new Talents(),
56+
TalentA = function(){},
57+
myObj = {},
58+
aMethod = function method(){};
59+
60+
TalentA.prototype.aMethod = aMethod;
61+
myObj.foo = 1;
62+
63+
sut.setParameter([{talent: TalentA}]);
64+
65+
sut.process(myObj);
66+
67+
it('makes the TalentA methods part of the given myObj', function(){
68+
expect(myObj).to.respondTo('aMethod');
69+
expect(myObj.aMethod).to.be.equal(aMethod);
70+
});
71+
});
72+
73+
describe('Passing a Talent reference `@talents:[TalentA]` to a Class', function(){
74+
var sut = new Talents(),
75+
TalentA = function(){},
76+
MyClass = function(){},
77+
aMethod = function method(){};
78+
79+
TalentA.prototype.aMethod = aMethod;
80+
MyClass.prototype.foo = 1;
81+
82+
sut.setParameter([TalentA]);
83+
84+
sut.process(MyClass);
85+
86+
it('makes the TalentA methods part of the given MyClass as static method', function(){
87+
expect(MyClass).to.not.respondTo('aMethod');
88+
expect(MyClass.aMethod).to.be.equal(aMethod);
89+
});
90+
});
91+
92+
});
93+
});

0 commit comments

Comments
 (0)