Class: CopyMethod::CopiedMethod

Inherits:
Module
  • Object
show all
Defined in:
lib/copy_method/copied_method.rb

Overview

Preserve constant lookups in copied methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, method_name, singleton: false) ⇒ CopiedMethod

Returns a new instance of CopiedMethod.

Parameters:

  • method_name (Symbol)
  • origin (Class)
  • singleton (Boolean) (defaults to: false)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/copy_method/copied_method.rb', line 18

def initialize(origin, method_name, singleton: false)
  @method_name  = method_name
  @origin       = origin.to_s.to_sym
  @singleton    = singleton

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def const_missing(const_name)
      caller_method = caller_locations(1,1)[0].label

      super
    rescue NameError => e
      origin = ::#{origin}

      if caller_method == #{method_name.to_s.inspect} && origin.const_defined?(const_name)
        origin.const_get const_name
      else
        raise e
      end
    end
  RUBY
end

Instance Attribute Details

#method_nameSymbol (readonly)

Returns:

  • (Symbol)


6
7
8
# File 'lib/copy_method/copied_method.rb', line 6

def method_name
  @method_name
end

#originSymbol (readonly)

Returns:

  • (Symbol)


10
11
12
# File 'lib/copy_method/copied_method.rb', line 10

def origin
  @origin
end

#singletonObject (readonly) Also known as: singleton?

Returns the value of attribute singleton.



12
13
14
# File 'lib/copy_method/copied_method.rb', line 12

def singleton
  @singleton
end

Instance Method Details

#inspectObject



40
41
42
# File 'lib/copy_method/copied_method.rb', line 40

def inspect
  "CopyMethod::CopiedMethod(#{origin}#{method_symbol}#{method_name})"
end