Class: PublicForwarder

Inherits:
BasicObject
Defined in:
lib/api_hammer/public_instance_exec.rb

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ PublicForwarder

Returns a new instance of PublicForwarder.



2
3
4
# File 'lib/api_hammer/public_instance_exec.rb', line 2

def initialize(object)
  @object=object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

forwards public methods to the object. attempting to invoke private or protected methods raises, as it would if the object was a normal receiver.



8
9
10
11
12
13
14
15
16
# File 'lib/api_hammer/public_instance_exec.rb', line 8

def method_missing(method, *args, &block)
  if @object.protected_methods.any?{|pm| pm.to_s == method.to_s }
    ::Kernel.raise ::NoMethodError, "protected method `#{method}' called for #{@object.inspect}"
  elsif @object.private_methods.any?{|pm| pm.to_s == method.to_s }
    ::Kernel.raise ::NoMethodError, "private method `#{method}' called for #{@object.inspect}"
  else
    @object.__send__(method, *args, &block)
  end
end