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