麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Ruby > 正文

ruby元編程之創建自己的動態方法

2020-03-29 12:01:35
字體:
來源:轉載
供稿:網友

method_missing是Ruby元編程(metaprogramming)常用的手法。基本思想是通過實現調用不存在的方法,以便進行回調。典型的例子是:ActiveRecord的動態查找(dynamic finder)。例如:我們有email屬性那么就可以調用User.find_by_email('[email protected]'),雖然, ActiveRecord::Base并沒有一個叫做find_by_email的方法。

respond_to? 并不如method_missing出名,常用在當需要確認一個回饋對象需要確認,以便不會因為沒有反饋對象,而導致后面的調用出現錯誤。

下面是一個應用這兩者的例子:

示例

我們有類Legislator class,現在,想要給它加一個find_by_first_name('John')的動態調用。實現find(:first_name => 'John')的功能。

復制代碼 代碼如下:

class Legislator
  #假設這是一個真實的實現
  def find(conditions = {})
  end
 
  #在本身定義畢竟這是他的方法
  def self.method_missing(method_sym, *arguments, &block)
    # the first argument is a Symbol, so you need to_s it if you want to pattern match
    if method_sym.to_s =~ /^find_by_(.*)$/
      find($1.to_sym => arguments.first)
    else
      super
    end
  end
end

 

那么這個時候調用

 

復制代碼 代碼如下:

Legislator.respond_to?(:find_by_first_name) 

 

將會提示錯誤,那么繼續

復制代碼 代碼如下:

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

 

正如代碼注釋所述respond_to?需要兩個參數,如果,你沒有提供將會產生ArgumentError。

相關反射 DRY

如果我們注意到了這里有重復的代碼。我們可以參考ActiveRecord的實現封裝在ActiveRecord::DynamicFinderMatch,以便避免在method_missing和respond_to?中重復。

復制代碼 代碼如下:

class LegislatorDynamicFinderMatch
  attr_accessor :attribute
  def initialize(method_sym)
    if method_sym.to_s =~ /^find_by_(.*)$/
      @attribute = $1.to_sym
    end
  end
 
  def match?
    @attribute != nil
  end
end

 

class Legislator
  def self.method_missing(method_sym, *arguments, &block)
    match = LegislatorDynamicFinderMatch.new(method_sym)
    if match.match?
      find(match.attribute => arguments.first)
    else
      super
    end
  end

  def self.respond_to?(method_sym, include_private = false)
    if LegislatorDynamicFinderMatch.new(method_sym).match?
      true
    else
      super
    end
  end
end

 

緩存 method_missing

重復多次的method_missing可以考慮緩存。

另外一個我們可以向ActiveRecord 學習的是,當定義method_missing的時候,發送 now-defined方法。如下:

復制代碼 代碼如下:

class Legislator   
  def self.method_missing(method_sym, *arguments, &block)
    match = LegislatorDynamicFinderMatch.new(method_sym)
    if match.match?
      define_dynamic_finder(method_sym, match.attribute)
      send(method_sym, arguments.first)
    else
      super
    end
  end
 
  protected
 
  def self.define_dynamic_finder(finder, attribute)
    class_eval <<-RUBY
      def self.#{finder}(#{attribute})        # def self.find_by_first_name(first_name)
        find(:#{attribute} => #{attribute})   #   find(:first_name => first_name)
      end                                     # end
    RUBY
  end
end

 

測試

測試部分如下:

復制代碼 代碼如下:

describe LegislatorDynamicFinderMatch do
  describe 'find_by_first_name' do
    before do
      @match = LegislatorDynamicFinderMatch.new(:find_by_first_name)
    end
     
    it 'should have attribute :first_name' do
      @match.attribute.should == :first_name
    end
   
    it 'should be a match' do
      @match.should be_a_match
    end
  end
 
  describe 'zomg' do
    before do
      @match = LegislatorDynamicFinderMatch(:zomg)
    end
   
    it 'should have nil attribute' do
      @match.attribute.should be_nil
    end
   
    it 'should not be a match' do
      @match.should_not be_a_match
    end
  end
end

 

下面是 RSpec 例子:

 

復制代碼 代碼如下:

describe Legislator, 'dynamic find_by_first_name' do 
  it 'should call find(:first_name => first_name)' do 
    Legislator.should_receive(:find).with(:first_name => 'John') 
     
    Legislator.find_by_first_name('John') 
  end 
end

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青青草最新网址 | 亚洲日韩精品欧美一区二区 | 一级黄色大片在线观看 | av免费在线网 | 在线天堂中文在线资源网 | 国产xxxx免费 | 成人一级片毛片 | 中文字幕视频在线播放 | 色综合久久久久久久久久久 | 成人性视频在线 | 久久久一区二区精品 | 色阁阁69婷婷 | 羞羞视频免费网站 | 91精品国产一区二区在线观看 | 久章草影院 | 久久人人爽人人爽人人片av高请 | 99re热视频这里只精品 | 欧美 日韩 中文 | 久久精品中文字幕一区二区 | 羞羞视频免费网站含羞草 | 欧美性视频一区二区 | 免费视频a | 天天躁狠狠躁夜躁2020挡不住 | 色av成人天堂桃色av | av手机免费在线观看 | 一级电影在线观看 | 精品国产91久久久 | 久久影片 | 污视频在线免费播放 | 永久久久 | 天天干天天透 | jizzjizz中国少妇中文 | 久久新地址 | 亚洲aⅴ免费在线观看 | 日本免费aaa观看 | 全黄裸片武则天艳史 | 成人一级视频 | 中国洗澡偷拍在线播放 | 全黄裸片武则天一级第4季 偿还电影免费看 | 国产精品一区在线观看 | 午夜国产在线观看 |