一个简易的ruby AOP框架 。

在开发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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值