Skip to content

Commit

Permalink
It is not necessary to use the returned value in Class.refactor.
Browse files Browse the repository at this point in the history
* Also change make Class.refactor a bit smaller by using a ternary instead of if-else.
* Update Class.refactor Specs
  • Loading branch information
Arian committed Feb 7, 2011
1 parent edeed7d commit d60e2ea
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Docs/Class/Class.Refactor.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Class.refactor, however, allows you to reference the previous state with *this.p
}
});

Cat = Class.refactor(Cat, {
Class.refactor(Cat, {
eat: function(){
this.previous(); //energy++!
alert("this cat has " + this.energy + " energy");
Expand Down
18 changes: 7 additions & 11 deletions Source/Class/Class.Refactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ Class.refactor = function(original, refactors){
Object.each(refactors, function(item, name){
var origin = original.prototype[name];
if (origin && origin.$origin) origin = origin.$origin;
if (typeof item == 'function'){
original.implement(name, function(){
var old = this.previous;
this.previous = origin || function(){};
var value = item.apply(this, arguments);
this.previous = old;
return value;
});
} else {
original.implement(name, item);
}
original.implement(name, (typeof item == 'function') ? function(){
var old = this.previous;
this.previous = origin || function(){};
var value = item.apply(this, arguments);
this.previous = old;
return value;
} : item);
});

return original;
Expand Down
51 changes: 25 additions & 26 deletions Specs/1.3/Class/Class.Refactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ License:
MIT-style license.
*/

(function(){
describe('Class.Refactor', function(){

var Test = new Class({
options: {
foo: 'bar',
Expand All @@ -20,7 +21,7 @@ License:
}
});
Test.static_method = function(){ return 'static';};
Test = Class.refactor(Test, {
Class.refactor(Test, {
options: { foo: 'rab' },
altered: function(){
return 'this is ' + this.previous();
Expand All @@ -31,12 +32,12 @@ License:
return 'altered';
}
});
Test2 = Class.refactor(Test2, {
Class.refactor(Test2, {
altered: function(){
return 'this is ' + this.previous();
}
});
Test2 = Class.refactor(Test2, {
Class.refactor(Test2, {
altered: function(){
return this.previous() + ' for reals.';
}
Expand All @@ -46,37 +47,35 @@ License:
Test3.prototype.origin = function(){
return "original origin";
};
Test3 = Class.refactor(Test3, {
Class.refactor(Test3, {
origin: function(){
return "refactored origin " + this.previous();
}
});

describe('Class.Refactor', {

'should return a method that has been altered twice': function(){
expect(new Test2().altered()).toEqual('this is altered for reals.');
},

'should return an unaltered method': function(){
expect(new Test().untouched()).toEqual('untouched');
},
it('should return a method that has been altered twice', function(){
expect(new Test2().altered()).toEqual('this is altered for reals.');
});

'should return an altred method': function(){
expect(new Test().altered()).toEqual('this is altered');
},
it('should return an unaltered method', function(){
expect(new Test().untouched()).toEqual('untouched');
});

'should return an altered property': function(){
expect(new Test().options.foo).toEqual('rab');
},
it('should return an altred method', function(){
expect(new Test().altered()).toEqual('this is altered');
});

'should return an unaltered property': function(){
expect(new Test().options.something).toEqual('else');
},
it('should return an altered property', function(){
expect(new Test().options.foo).toEqual('rab');
});

'should return the original origin': function(){
expect(new Test3().origin()).toEqual('refactored origin original origin');
}
it('should return an unaltered property', function(){
expect(new Test().options.something).toEqual('else');
});

it('should return the original origin', function(){
expect(new Test3().origin()).toEqual('refactored origin original origin');
});
})();

});

0 comments on commit d60e2ea

Please sign in to comment.