Module: Kernel::MethodMissingDefinition

Defined in:
lib/method_matching.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/method_matching.rb', line 19

def method_missing(method_name, *args, &block)
  try_matcher = Proc.new do |matcher, mdef|
    if method_name.to_s =~ matcher
      mdef.block = block
      return mdef.call(method_name, *args)
    end
  end
  
  method_matchers.each { |m, mdef| try_matcher.call(m, mdef) }
  self.class.method_matchers.each { |m, mdef| try_matcher.call(m, mdef) }
  super
end

Instance Method Details

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/method_matching.rb', line 32

def respond_to?(method_name)
  (method_matchers.keys +
   self.class.method_matchers.keys).each do |matcher|
    return true if method_name.to_s =~ matcher
  end
  super
end