2007-12-14
一个简易的ruby AOP框架 。
在开发smart memcache的过程中 ,开发了一个简易的 ruby aop 。
支持 before,after,rescue 的拦截,支持重复,任意组合的拦截声明 。
在后续的版本支持 arround interceptor声明。
基本的使用代码如下:
after 拦截使用:
说明:
aop_info 为aop的元数据,类型为hash。 主要元数据:
*args 为被拦截的方法调用的参数。
before 拦截使用:
:interceptor_type=>:before 由after改为 before
rescue同样的 : :interceptor_type=>:rescue
rescue的话,aopinfo里面可以通过 :error获取到异常信息。
后续版本会做一个arround interceptor的支持 ,大概使用例子如下:
其中arround_statement_str里面的原来方法的调用必须使用一个特殊的标志串 :
AOP_ORIGINAL_METHOD_CALL
支持 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
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 22307 次
- 性别:


- 详细资料
搜索本博客
最近加入圈子
最新评论
-
谈谈应用ORM框架针对遗留 ...
BaseService extends HibernateDAOSupport? ...
-- by sslaowan -
谁了解Paulo提出的String ...
可以用google scholar
-- by tiantian911 -
关于实现一个rails smart ...
nihongye 写道firebody 写道LRU频繁的话,性能应该会很差 不知 ...
-- by firebody -
关于实现一个rails smart ...
firebody 写道LRU频繁的话,性能应该会很差 不知道这个猜测是从哪里来的 ...
-- by nihongye -
谈谈应用ORM框架针对遗留 ...
我也经常会为了少写一些代码而使用继承,而不是用工具类,这样会在心里上有一种更直观 ...
-- by downpour






评论排行榜