在开发smart memcache的过程中 ,开发了一个简易的 ruby aop 。
支持 before,after,rescue 的拦截,支持重复,任意组合的拦截声明 。
在后续的版本支持 arround interceptor声明。
基本的使用代码如下:
after 拦截使用:
[code]
@ruby_aop = AOP::RubyAop.new
@ruby_aop.interceptor(:classes=>["User","Teacher"],:methods=>[:update_all],:interceptor_type=>:after) { |aop_info,*args| # your after block here }[/code]
说明:
aop_info 为aop的元数据,类型为hash。 主要元数据:
[code]
@aop_inf[:intercepted_class] 被拦截的类名(quanlified class name)
@aop_inf[:intercepted_method] 被拦截的方法名(symbol)
@aop_inf[:intercepted_method_is_class_method] 是否类方法) (true|false)[/code]
*args 为被拦截的方法调用的参数。
before 拦截使用:
[code]@ruby_aop = AOP::RubyAop.new
@ruby_aop.interceptor(:classes=>["User","Teacher"],:methods=>[:update_all],:interceptor_type=>:before) { |aop_info,*args| # your after block here }[/code]
:interceptor_type=>:before 由after改为 before
rescue同样的 : :interceptor_type=>:rescue
rescue的话,aopinfo里面可以通过 :error获取到异常信息。
后续版本会做一个arround interceptor的支持 ,大概使用例子如下:
[code]arround_statement_str=<<END
#hahahah before statement here
AOP_ORIGINAL_METHOD_CALL
##bla bla after statement here
END[/code]
[code]@ruby_aop = AOP::RubyAop.new
@ruby_aop.interceptor(:classes=>["User","Teacher"],:methods=>[:update_all],:interceptor_type=>:arround,:arround_body=>arround_statement_str)
[/code]
其中arround_statement_str里面的原来方法的调用必须使用一个特殊的标志串 :
AOP_ORIGINAL_METHOD_CALL
支持 before,after,rescue 的拦截,支持重复,任意组合的拦截声明 。
在后续的版本支持 arround interceptor声明。
基本的使用代码如下:
after 拦截使用:
[code]
@ruby_aop = AOP::RubyAop.new
@ruby_aop.interceptor(:classes=>["User","Teacher"],:methods=>[:update_all],:interceptor_type=>:after) { |aop_info,*args| # your after block here }[/code]
说明:
aop_info 为aop的元数据,类型为hash。 主要元数据:
[code]
@aop_inf[:intercepted_class] 被拦截的类名(quanlified class name)
@aop_inf[:intercepted_method] 被拦截的方法名(symbol)
@aop_inf[:intercepted_method_is_class_method] 是否类方法) (true|false)[/code]
*args 为被拦截的方法调用的参数。
before 拦截使用:
[code]@ruby_aop = AOP::RubyAop.new
@ruby_aop.interceptor(:classes=>["User","Teacher"],:methods=>[:update_all],:interceptor_type=>:before) { |aop_info,*args| # your after block here }[/code]
:interceptor_type=>:before 由after改为 before
rescue同样的 : :interceptor_type=>:rescue
rescue的话,aopinfo里面可以通过 :error获取到异常信息。
后续版本会做一个arround interceptor的支持 ,大概使用例子如下:
[code]arround_statement_str=<<END
#hahahah before statement here
AOP_ORIGINAL_METHOD_CALL
##bla bla after statement here
END[/code]
[code]@ruby_aop = AOP::RubyAop.new
@ruby_aop.interceptor(:classes=>["User","Teacher"],:methods=>[:update_all],:interceptor_type=>:arround,:arround_body=>arround_statement_str)
[/code]
其中arround_statement_str里面的原来方法的调用必须使用一个特殊的标志串 :
AOP_ORIGINAL_METHOD_CALL