欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2015年05月26日 09:24:09   投稿:junjie  
這篇文章主要介紹了ruby元編程之創(chuàng)建自己的動態(tài)方法,本文講解使用method_missing和respond_to?創(chuàng)建自己的動態(tài)方法,需要的朋友可以參考下

method_missing是Ruby元編程(metaprogramming)常用的手法?;舅枷胧峭ㄟ^實現(xiàn)調(diào)用不存在的方法,以便進行回調(diào)。典型的例子是:ActiveRecord的動態(tài)查找(dynamic finder)。例如:我們有email屬性那么就可以調(diào)用User.find_by_email('joe@example.com'),雖然, ActiveRecord::Base并沒有一個叫做find_by_email的方法。

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

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

示例

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

復制代碼 代碼如下:

class Legislator
  #假設(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

那么這個時候調(diào)用

復制代碼 代碼如下:

Legislator.respond_to?(:find_by_first_name) 

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

復制代碼 代碼如下:

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

相關(guān)反射 DRY

如果我們注意到了這里有重復的代碼。我們可以參考ActiveRecord的實現(xiàn)封裝在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的時候,發(fā)送 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

相關(guān)文章

  • Ruby語言中的String深入理解

    Ruby語言中的String深入理解

    在java中,對于字面量的字符串,jvm內(nèi)部維持一張表,因此如果在java中,str1和str2是同一個String對象。而在Ruby中, str1和str2是完全不同的對象。
    2011-01-01
  • RUBY文檔中心-學習開始

    RUBY文檔中心-學習開始

    RUBY文檔中心-學習開始...
    2007-11-11
  • 實例講解Ruby中的五種變量

    實例講解Ruby中的五種變量

    這篇文章主要介紹了Ruby中的五種變量,并用實例講解了其用法,是Ruby學習當中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-04-04
  • Ruby中require、load、include、extend的區(qū)別介紹

    Ruby中require、load、include、extend的區(qū)別介紹

    這篇文章主要介紹了Ruby中require、load、include、extend的區(qū)別介紹,require、load用于文件,如.rb等等結(jié)尾的文件,include、load則用于包含一個文件中的模塊,需要的朋友可以參考下
    2015-05-05
  • redis集群搭建教程及遇到的問題處理

    redis集群搭建教程及遇到的問題處理

    本文主要給大家講訴的是如何搭建redis集群的方法以及在此過程中需要注意的問題,非常不錯,推薦給大家,有需要的小伙伴可以來參考下
    2017-09-09
  • Ruby連接使用windows下sql server數(shù)據(jù)庫代碼實例

    Ruby連接使用windows下sql server數(shù)據(jù)庫代碼實例

    這篇文章主要介紹了Ruby連接使用windows下sql server數(shù)據(jù)庫代碼實例,本文直接給出實現(xiàn)代碼,而且給出了兩種實現(xiàn)和access數(shù)據(jù)庫的實現(xiàn)代碼,需要的朋友可以參考下
    2015-05-05
  • Ruby程序中創(chuàng)建和解析XML文件的方法

    Ruby程序中創(chuàng)建和解析XML文件的方法

    這篇文章主要介紹了Ruby程序中創(chuàng)建和解析XML文件的方法,創(chuàng)建用builder庫,解析用ReXML庫,需要的朋友可以參考下
    2015-11-11
  • Ruby初學筆記之Hello World

    Ruby初學筆記之Hello World

    這篇文章主要介紹了Ruby初學筆記之Hello World,本文是一篇Ruby自學筆記,本文分別用普通方法、函數(shù)、類輸出Hello World,從一個一個的例子中了解Ruby編程,需要的朋友可以參考下
    2015-06-06
  • 簡單對比分析Ruby on Rails 和 Laravel

    簡單對比分析Ruby on Rails 和 Laravel

    web應用程序開發(fā)中兩個相對而言更加流行的框架是 Ruby on Rails 和 Laravel. 它們兩個都是非常成熟的項目,已經(jīng)面世相當長一段時間了 .
    2014-07-07
  • 幾個加速Ruby on Rails的編程技巧

    幾個加速Ruby on Rails的編程技巧

    這篇文章主要介紹了幾個加速Ruby on Rails的編程技巧,本文來自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下
    2015-04-04

最新評論