Skip to content
Browse files

It is not necessary to use the returned value in Class.refactor.

* Also change make Class.refactor a bit smaller by using a ternary instead of if-else.
* Update Class.refactor Specs
  • Loading branch information...
1 parent edeed7d commit d60e2ea094863aee029164cee7bfd952937e701e @arian arian committed Feb 7, 2011
Showing with 33 additions and 38 deletions.
  1. +1 −1 Docs/Class/Class.Refactor.md
  2. +7 −11 Source/Class/Class.Refactor.js
  3. +25 −26 Specs/1.3/Class/Class.Refactor.js
View
2 Docs/Class/Class.Refactor.md
@@ -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");
View
18 Source/Class/Class.Refactor.js
@@ -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;
View
51 Specs/1.3/Class/Class.Refactor.js
@@ -6,7 +6,8 @@ License:
MIT-style license.
*/
-(function(){
+describe('Class.Refactor', function(){
+
var Test = new Class({
options: {
foo: 'bar',
@@ -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();
@@ -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.';
}
@@ -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.
Something went wrong with that request. Please try again.