Class: Methods::MethodsWrapper

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

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ 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, &block) ⇒ Object



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

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

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

Instance Method Details

#respond_to?(method) ⇒ Boolean



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

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



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

def respond_to_missing?(*args)
  true
end