Module: BadBill::ForwardMethods

Included in:
BaseResource
Defined in:
lib/badbill/forward_methods.rb

Overview

Forward all methods to an underlying object called data.

This acts like a proxy object.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Respond to method_missing to send down the line.

As Hashie::Mash#method_missing does not respond with true to an assignment request, we only check for the method name without the equal sign.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/badbill/forward_methods.rb', line 12

def method_missing(method_name, *arguments, &block)
  if data.respond_to?(method_name.to_s.sub(/=$/, ''))
    if method_name.to_s.end_with?('=')
      @__mutated__ ||= []
      @__mutated__ << method_name.to_s.sub(/=$/, '')
      @__mutated__.uniq!
    end

    data.send(method_name, *arguments, &block)
  else
    super
  end
end

Instance Method Details

#respond_to?(method_name, include_private = false) ⇒ Boolean

Proxy respond_to? to the underlying object if needed.

If playing with method_missing define respond_to? too. See robots.thoughtbot.com/post/28335346416/always-define-respond-to-missing-when-overriding

Returns:

  • (Boolean)

    True if the class itself or the proxied object responds to the given method.



32
33
34
# File 'lib/badbill/forward_methods.rb', line 32

def respond_to?(method_name, include_private = false)
  super || data.respond_to?(method_name, include_private)
end