使用method_missing和respond_to?创建自己的动态方法


method_missing是Ruby元编程(metaprogramming)常用的手法。基本思想是通过实现调用不存在的方法,以便进行回调。典型的例子是:ActiveRecord的动态查找(dynamic finder)。例如:我们有email属性那么就可以调用User.find_by_email('joe@example.com'),虽然, ActiveRecord::Base并没有一个叫做find_by_email的方法。

respond_to? 并不如method_missing出名,常用在当需要确认一个回馈对象需要确认,以便不会因为没有反馈对象,而导致后面的调用出现错误。

下面是一个应用这两者的例子:

示例
我们有类Legislator class,现在,想要给它加一个find_by_first_name('John')的动态调用。实现find(:first_name => 'John')的功能。
Ruby代码   收藏代码
  1. class Legislator    
  2.   #假设这是一个真实的实现    
  3.   def find(conditions = {})    
  4.   end    
  5.       
  6.   #在本身定义毕竟这是他的方法    
  7.   def self.method_missing(method_sym, *arguments, &block)    
  8.     # the first argument is a Symbol, so you need to_s it if you want to pattern match    
  9.     if method_sym.to_s =~ /^find_by_(.*)$/    
  10.       find($1.to_sym => arguments.first)    
  11.     else    
  12.       super    
  13.     end    
  14.   end    
  15. end    

那么这个时候调用
Ruby代码

  1. Legislator.respond_to?(:find_by_first_name)    

将会提示错误,那么继续
Ruby代码 

  1. class Legislator    
  2.   # 省略    
  3.       
  4.   # It's important to know Object defines respond_to to take two parameters: the method to check, and whether to include private methods    
  5.   # http://www.ruby-doc.org/core/classes/Object.html#M000333    
  6.   def self.respond_to?(method_sym, include_private = false)    
  7.     if method_sym.to_s =~ /^find_by_(.*)$/    
  8.       true    
  9.     else    
  10.       super    
  11.     end    
  12.   end    
  13. end    

正如代码注释所述respond_to?需要两个参数,如果,你没有提供将会产生ArgumentError。
相关反射 DRY
如果我们注意到了这里有重复的代码。我们可以参考ActiveRecord的实现封装在ActiveRecord::DynamicFinderMatch,以便避免在method_missing和respond_to?中重复。

Ruby代码  
  1. class LegislatorDynamicFinderMatch    
  2.   attr_accessor :attribute    
  3.   def initialize(method_sym)    
  4.     if method_sym.to_s =~ /^find_by_(.*)$/    
  5.       @attribute = $1.to_sym    
  6.     end    
  7.   end    
  8.       
  9.   def match?    
  10.     @attribute != nil    
  11.   end    
  12. end    
  13.     
  14. class Legislator    
  15.   def self.method_missing(method_sym, *arguments, &block)    
  16.     match = LegislatorDynamicFinderMatch.new(method_sym)    
  17.     if match.match?    
  18.       find(match.attribute => arguments.first)    
  19.     else    
  20.       super    
  21.     end    
  22.   end    
  23.     
  24.   def self.respond_to?(method_sym, include_private = false)    
  25.     if LegislatorDynamicFinderMatch.new(method_sym).match?    
  26.       true    
  27.     else    
  28.       super    
  29.     end    
  30.   end    
  31. end    

缓存 method_missing
重复多次的method_missing可以考虑缓存。

另外一个我们可以向ActiveRecord 学习的是,当定义method_missing的时候,发送 now-defined方法。如下:
Ruby代码  
  1. class Legislator        
  2.   def self.method_missing(method_sym, *arguments, &block)    
  3.     match = LegislatorDynamicFinderMatch.new(method_sym)    
  4.     if match.match?    
  5.       define_dynamic_finder(method_sym, match.attribute)    
  6.       send(method_sym, arguments.first)    
  7.     else    
  8.       super    
  9.     end    
  10.   end    
  11.       
  12.   protected    
  13.       
  14.   def self.define_dynamic_finder(finder, attribute)    
  15.     class_eval <<-RUBY    
  16.       def self.#{finder}(#{attribute})        # def self.find_by_first_name(first_name)    
  17.         find(:#{attribute} => #{attribute})   #   find(:first_name => first_name)    
  18.       end                                     # end    
  19.     RUBY    
  20.   end    
  21. end    

测试


测试部分如下:
Ruby代码  
  1. describe LegislatorDynamicFinderMatch do    
  2.   describe 'find_by_first_name' do    
  3.     before do    
  4.       @match = LegislatorDynamicFinderMatch.new(:find_by_first_name)    
  5.     end    
  6.           
  7.     it 'should have attribute :first_name' do    
  8.       @match.attribute.should == :first_name    
  9.     end    
  10.         
  11.     it 'should be a match' do    
  12.       @match.should be_a_match    
  13.     end    
  14.   end    
  15.       
  16.   describe 'zomg' do    
  17.     before do    
  18.       @match = LegislatorDynamicFinderMatch(:zomg)    
  19.     end    
  20.         
  21.     it 'should have nil attribute' do    
  22.       @match.attribute.should be_nil    
  23.     end    
  24.         
  25.     it 'should not be a match' do    
  26.       @match.should_not be_a_match    
  27.     end    
  28.   end    
  29. end    


下面是 RSpec 例子:


Ruby代码  
  1. describe Legislator, 'dynamic find_by_first_name' do    
  2.   it 'should call find(:first_name => first_name)' do    
  3.     Legislator.should_receive(:find).with(:first_name => 'John')    
  4.         
  5.     Legislator.find_by_first_name('John')    
  6.   end    
  7. end    

Summary

如果,你打算使用method_missing,那么建议,考虑respond_to?。这将减少代码重复,提示性能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值