ITeye博客

关于Ruby Aop的小试验

nacu · 2007-06-21 13:49 · ruby · Google · Ruby · AOP · 编程 · ActiveRecord · Markdown

Ruby 的Metaclass功能确实强大,
我仿照Rail的处理的做了一个Aop 的小试验,代码如下:

ruby 代码

 

  1. # aop_test.rb
  2. # 2007年6月18日
  3. #
  4. module AopAspects
  5. def log_method(*args)
  6. puts "log here"
  7. args.each do |arg|
  8. puts arg.to_s
  9. end
  10. end
  11. def trans_aspect(*args)
  12. puts "trans here"
  13. end
  14. end
  15. module AopBinder
  16. def self.included(c)
  17. c.extend(AopGenerate)
  18. end
  19. module AopGenerate
  20. def run_before(method,*aspects)
  21. alias_method "#{method}_old".to_sym, method
  22. before_method = ""
  23. aspects.inject("") {|before_method,aspect| before_method<<(aspect.to_s + "(*args)\n")}
  24. class_eval <<-END
  25. def #{method}(*args)
  26. #{before_method}
  27. #{method}_old(*args)
  28. end
  29. END
  30. end
  31. def run_after(method,*aspects)
  32. alias_method "#{method}_old".to_sym, method
  33. before_method = ""
  34. aspects.inject("") {|before_method,aspect| before_method<<(aspect.to_s + "(*args)\n")}
  35. class_eval <<-END
  36. def #{method}(*args)
  37. #{method}_old(*args)
  38. #{before_method}
  39. end
  40. END
  41. end
  42. end
  43. end
  44. class AopTest
  45. include AopAspects
  46. include AopBinder
  47. def initialize
  48. end
  49. def run
  50. puts "hello, my run"
  51. end
  52. def run2(name="")
  53. puts "hello, #{name}"
  54. end
  55. run_before :run ,:log_method
  56. run_after :run2 , :log_method ,:trans_aspect
  57. end
  58. a = AopTest.new
  59. a.run
  60. a.run2("ben")

结果如下
log here
hello, my run
hello, ben
log here
ben
trans here

充分体现了Ruby 简洁锐利的原编程风格

评论(10)

  1. #10 楼 · nacu · 2007-06-22 14:59

    谢谢hideto 学到了很多阿 这样确实实现了最大的灵活度

  2. #9 楼 · hideto · 2007-06-22 13:54

    nacu writes:

    对阿 呵呵 没有想到 对于 block的使用还是不太习惯 这样的实现是最简洁的 按照ruby 的惯例 最后的使用应该是 run_before :method1 ,:method2 ...........{do something} run_after :method1 ,:method2 ...........{do something} run_around :method1 ,:method2 ...........{do something} 这样的描述吧

    参考一下ActiveRecord的Callbacks,共有四种macros,分别为Method references(symbol)/Callback objects/Inline methods(proc)/Inline eval methods(string)

    
    case callback   
                when Symbol   
                  self.send(callback)   
                when String   
                  eval(callback, binding)   
                when Proc, Method   
                  callback.call(self)   
                else  
                  if callback.respond_to?(method)   
                    callback.send(method, self)   
    
    

    http://hideto.iteye.com/blog/93084

  3. #8 楼 · nacu · 2007-06-22 12:54

    和下面的表述是一样的 class_eval(%Q) ruby 的语法还真是,实现同一种目的有n种表述方法,在项目应用中看来还是要制定一种表述约定,不然沟通和维护实在是不方便。

  4. #7 楼 · edge_hh · 2007-06-22 12:48

    nacu writes:

    不好意思 <<->END 的表述不太好 可能用 class_eval <<->EOF def #{method}(*args) #{before_method} #{method}_old(*args) end EOF 更加好吧

    表示一个字符块

    真的不好意思。能解释一下字符块么?或者给个链接 我google了 ruby 字符块 也没结果。

    谢谢了

  5. #6 楼 · nacu · 2007-06-22 11:37

    对阿 呵呵 没有想到 对于 block的使用还是不太习惯 这样的实现是最简洁的 按照ruby 的惯例 最后的使用应该是 run_before :method1 ,:method2 ...........{do something} run_after :method1 ,:method2 ...........{do something} run_around :method1 ,:method2 ...........{do something} 这样的描述吧

  6. #5 楼 · Readonly · 2007-06-22 11:26

    nacu writes:

    谢谢 readonly 的回复阿 有个小小的疑惑,如果用run_before + block 确认可以省去很多的代码,不过这个切面的实现就被放到block中了。问题是,切面的代码放在哪里才是最合适的呢?是不是应该有个地方专门用来定义这个东西的。 随着代码量的增加,按照DRY的原则,是不是应该继续抽象这个切面的实现方式,放到某个module中呢。

    有了block不就可以调用任何地方的代码了么,随便你把要分离的代码放到哪里都可以啊,Class Method, module mixin进来都可以:

    
    class Test    
      def run  
        puts "hello, my run"    
      end    
        
      def self.log
        puts "before run"
      end
      
      run_before(:run) {log}
    end
    
    
  7. #4 楼 · nacu · 2007-06-22 11:18

    不好意思 <<->END 的表述不太好 可能用 class_eval <<->EOF def #{method}(*args) #{before_method} #{method}_old(*args) end EOF 更加好吧

    表示一个字符块

  8. #3 楼 · edge_hh · 2007-06-22 10:59

    这段代码没看懂

    class_eval <<->END def #{method}(*args)
    #{before_method}
    #{method}_old(*args)
    end
    END

    :(

    END是啥?

  9. #2 楼 · nacu · 2007-06-22 10:24

    谢谢 readonly 的回复阿 有个小小的疑惑,如果用run_before + block 确认可以省去很多的代码,不过这个切面的实现就被放到block中了。问题是,切面的代码放在哪里才是最合适的呢?是不是应该有个地方专门用来定义这个东西的。 随着代码量的增加,按照DRY的原则,是不是应该继续抽象这个切面的实现方式,放到某个module中呢。

  10. #1 楼 · Readonly · 2007-06-22 10:09

    偶觉得动态语言没有必要啥正儿八经的Aop框架,利用现成的hotswap,mixin,method_missing这些特性,来做关注分离是很简单的。

    
    def run_before(m)
      alias_method "__before__#{m}", m
      define_method(m) {|*arg| yield(*arg); send("__before__#{m}", *arg);}
    end
    
    class Test  
      def run
        puts "hello, my run"  
      end  
      
      run_before(:run) {puts "before run"} 
    end
    
    

评论功能维护中,当前仅展示历史评论。