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

首頁 > 編程 > Ruby > 正文

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

2020-02-24 15:39:03
字體:
供稿:網(wǎng)友

最初的所謂Ruby元編程是一種可以在運行時動態(tài)地操縱語言結(jié)構(gòu)的技術(shù),您甚至可以輸入一個新的Ruby代碼并在運行時執(zhí)行它,而無需重新啟動它,需要的朋友可以進入下文參考一下。

示例

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

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

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)用

?

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

Legislator.respond_to?(:find_by_first_name)?

?

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

相關(guān)反射 DRY

如果我們注意到了這里有重復(fù)的代碼。我們可以參考ActiveRecord的實現(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可以考慮緩存。

另外一個我們可以向ActiveRecord 學(xué)習(xí)的是,當定義method_missing的時候,發(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

大家看完上文ruby元編程之創(chuàng)建自己的動態(tài)方法之后,知道要怎么操作了吧!總之,為了讓我們在工作中能順利,要多關(guān)注武林技術(shù)頻道分享的內(nèi)容哦!

?

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 欧美日韩中文字幕在线 | 成人男女视频 | 一本色道久久99精品综合蜜臀 | 九九热视频这里只有精品 | 91嫩草丨国产丨精品入口 | 国产日产精品一区二区三区四区 | 羞羞视频.www在线观看 | 日本精品久久久一区二区三区 | 亚洲资源在线 | 亚洲国产中文字幕 | 中国成人在线视频 | 一级做a爰性色毛片免费 | 亚洲精中文字幕二区三区 | 大学生一级毛片在线视频 | 一级国产免费 | 国产超碰人人做人人爱 | 九九热精品在线 | 国产在线精品一区二区三区 | 97久久日一线二线三线 | 久久艹精品视频 | 久久精品视频在线 | 免费一级电影 | 色网站免费观看 | 天天曰夜夜操 | 精品国产99久久久久久宅男i | 亚洲欧美日韩一区二区三区在线观看 | 在线日韩亚洲 | asian裸体佳人pics | 黄视频网站免费观看 | 在线a| 色网免费观看 | 欧美 国产 亚洲 卡通 综合 | 欧美日韩1区2区 | 香蕉秀| 日本在线视频二区 | 久久老司机 | 国产一及毛片 | 色女人在线 | 国产欧美一区二区三区免费看 | 日本aaaa片毛片免费观蜜桃 | 精品亚洲一区二区 |