Class: Methods::MethodsWrapper

Inherits:
BasicObject
Defined in:
lib/methods/methods_wrapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ MethodsWrapper

Returns a new instance of MethodsWrapper.



3
4
5
# File 'lib/methods/methods_wrapper.rb', line 3

def initialize(obj)
  @obj = obj
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/methods/methods_wrapper.rb', line 7

def method_missing(method, *args, **kwargs)
  __raise_no_method_error(method) unless respond_to?(method)
  method = @obj.method(method)

  if args.empty? && kwargs.empty?
    method
  else
    ::Kernel.lambda do |*call_args, **call_kwargs|
      merged_kwargs = kwargs.merge(**call_kwargs)
      if merged_kwargs.empty?
        method.call(*args, *call_args)
      else
        method.call(*args, *call_args, **kwargs)
      end
    end
  end
end

Instance Method Details

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'lib/methods/methods_wrapper.rb', line 25

def respond_to?(method)
  if ::Kernel.binding.of_caller(2).eval('self').object_id == @obj.object_id
    @obj.respond_to?(method) || @obj.private_methods.include?(method)
  else
    @obj.respond_to?(method)
  end
end

#respond_to_missing?(*args) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/methods/methods_wrapper.rb', line 33

def respond_to_missing?(*args)
  true
end