论坛首页 编程语言技术论坛

一个简易的ruby AOP框架 。

浏览 2331 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-12-14  
在开发smart memcache的过程中 ,开发了一个简易的 ruby aop 。

支持 before,after,rescue 的拦截,支持重复,任意组合的拦截声明 。

在后续的版本支持 arround interceptor声明。

基本的使用代码如下:
after 拦截使用:
@ruby_aop = AOP::RubyAop.new 
@ruby_aop.interceptor(:classes=>["User","Teacher"],:methods=>[:update_all],:interceptor_type=>:after) { |aop_info,*args| # your after block here }


说明:

aop_info 为aop的元数据,类型为hash。 主要元数据:
 @aop_inf[:intercepted_class]  被拦截的类名(quanlified class name)
 @aop_inf[:intercepted_method]  被拦截的方法名(symbol)
 @aop_inf[:intercepted_method_is_class_method] 是否类方法) (true|false)


*args 为被拦截的方法调用的参数。

before 拦截使用:
@ruby_aop = AOP::RubyAop.new 
@ruby_aop.interceptor(:classes=>["User","Teacher"],:methods=>[:update_all],:interceptor_type=>:before) { |aop_info,*args| # your after block here }


:interceptor_type=>:before 由after改为 before

rescue同样的 : :interceptor_type=>:rescue

rescue的话,aopinfo里面可以通过 :error获取到异常信息。

后续版本会做一个arround interceptor的支持 ,大概使用例子如下:
arround_statement_str=<<END
   #hahahah before statement here
   AOP_ORIGINAL_METHOD_CALL
   ##bla bla after statement here 
   
END


@ruby_aop = AOP::RubyAop.new 
@ruby_aop.interceptor(:classes=>["User","Teacher"],:methods=>[:update_all],:interceptor_type=>:arround,:arround_body=>arround_statement_str) 


其中arround_statement_str里面的原来方法的调用必须使用一个特殊的标志串 :
AOP_ORIGINAL_METHOD_CALL

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics