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

首頁 > 編程 > Ruby > 正文

ruby元編程之創(chuàng)建自己的動(dòng)態(tài)方法

2020-10-29 19:38:41
字體:
供稿:網(wǎng)友

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

respond_to? 并不如method_missing出名,常用在當(dāng)需要確認(rèn)一個(gè)回饋對象需要確認(rèn),以便不會因?yàn)闆]有反饋對象,而導(dǎo)致后面的調(diào)用出現(xiàn)錯(cuò)誤。

下面是一個(gè)應(yīng)用這兩者的例子:

示例

我們有類Legislator class,現(xiàn)在,想要給它加一個(gè)find_by_first_name('John')的動(dòng)態(tài)調(diào)用。實(shí)現(xiàn)find(:first_name => 'John')的功能。

復(fù)制代碼 代碼如下:

class Legislator
  #假設(shè)這是一個(gè)真實(shí)的實(shí)現(xiàn)
  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

那么這個(gè)時(shí)候調(diào)用

復(fù)制代碼 代碼如下:

Legislator.respond_to?(:find_by_first_name) 

將會提示錯(cuò)誤,那么繼續(xù)

復(fù)制代碼 代碼如下:

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?需要兩個(gè)參數(shù),如果,你沒有提供將會產(chǎn)生ArgumentError。

相關(guān)反射 DRY

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

復(fù)制代碼 代碼如下:

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

重復(fù)多次的method_missing可以考慮緩存。

另外一個(gè)我們可以向ActiveRecord 學(xué)習(xí)的是,當(dāng)定義method_missing的時(shí)候,發(fā)送 now-defined方法。如下:

復(fù)制代碼 代碼如下:

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

測試

測試部分如下:

復(fù)制代碼 代碼如下:

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 例子:

復(fù)制代碼 代碼如下:

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 美女91视频 | chinese乱子伦xxxx国语对白 | 国产精品视频不卡 | 成人毛片在线 | 一区二区三区欧美日韩 | 久久精品久久精品国产大片 | 一本色道久久综合狠狠躁篇适合什么人看 | 在线无码 | 一本色道久久综合狠狠躁篇适合什么人看 | 亚洲婷婷日日综合婷婷噜噜噜 | 成人午夜在线免费观看 | 成年免费视频黄网站在线观看 | 欧美特黄一级高清免费的香蕉 | 91在线色视频 | 9999久久 | 免费午夜视频 | 91av久久| 免费毛片视频 | 亚洲免费毛片基地 | 亚洲午夜网站 | 亚洲欧美日韩免费 | 国产精品免费一区二区三区四区 | 久久国产精品二区 | 国产一级大片 | 国产成人在线免费观看视频 | 国产成人精品免费视频大全最热 | 一二区成人影院电影网 | 久久久久久69 | 国产亚洲精品成人 | 久久久久国产精品久久久久 | 日韩中文字幕一区二区三区 | 色妞妞视频 | 双性精h调教灌尿打屁股的文案 | 13一14毛片免费看 | 91精品国产九九九久久久亚洲 | 91精品国产777在线观看 | 国产精品久久久久无码av | 欧美性生活视频免费看 | 免费一级特黄欧美大片勹久久网 | 国产一级二级在线播放 | 成人免费观看49www在线观看 |