Class: CopyMethod::MethodCopier Private

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/copy_method/method_copier.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

TODO:

Be clever about ‘ActiveSupport::Concern`, if we are copying a singleton method to such a module, check for / create the `ClassMethods` submodule and place the method on that.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

activesupport_concern?, assert_module!, looks_like_module?

Constructor Details

#initialize(name:, from:, to:, singleton: false, remove: false, singleton_target: nil) ⇒ MethodCopier

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of MethodCopier.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/copy_method/method_copier.rb', line 16

def initialize(name:, from:, to:, singleton: false, remove: false, singleton_target: nil)
  @method_name              = name
  @from                     = assert_module! from, :from
  @to                       = assert_module! to, :to
  @singleton                = singleton
  @remove                   = remove

  if singleton_target.nil?
    @singleton_target = to_module? ? false : singleton
  else
    @singleton_target = !!singleton_target
  end
end

Instance Attribute Details

#fromObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
# File 'lib/copy_method/method_copier.rb', line 9

def from
  @from
end

#method_nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
# File 'lib/copy_method/method_copier.rb', line 9

def method_name
  @method_name
end

#removeObject (readonly) Also known as: remove?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
# File 'lib/copy_method/method_copier.rb', line 9

def remove
  @remove
end

#singletonObject (readonly) Also known as: singleton?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
# File 'lib/copy_method/method_copier.rb', line 9

def singleton
  @singleton
end

#singleton_targetObject (readonly) Also known as: singleton_target?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
# File 'lib/copy_method/method_copier.rb', line 9

def singleton_target
  @singleton_target
end

#toObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
# File 'lib/copy_method/method_copier.rb', line 9

def to
  @to
end

Instance Method Details

#class_receiver?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


89
90
91
# File 'lib/copy_method/method_copier.rb', line 89

def class_receiver?
  singleton_receiver? && singleton_receiver != 'self'
end

#convert_to_instance_method?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This checks for the condition where we want to copy a ‘Class`’s singleton method to a ‘Module`’s instance methods. By default, the library assumes that you would want to copy a method for any ‘Class` that then extends the target module.

To override this logic, explicitly set ‘#singleton_target` to `true` if providing a module to `#to`.

Returns:

  • (Boolean)


114
115
116
# File 'lib/copy_method/method_copier.rb', line 114

def convert_to_instance_method?
  singleton? && to_module? && !from_module? && !singleton_target?
end

#copy!Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Where the magic happens.

Returns:

  • (Symbol)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/copy_method/method_copier.rb', line 56

def copy!
  location, line = definition.source_location

  target.module_eval corrected_source, location, line

  attach_helper_module!

  remove_from_origin! if remove?

  return method_name
end

#corrected_sourceString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


94
95
96
# File 'lib/copy_method/method_copier.rb', line 94

def corrected_source
  correct_source_receiver method_source
end

#definitionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
# File 'lib/copy_method/method_copier.rb', line 30

def definition
  @_definition ||= fetch_method_definition
end

#from_module?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


98
99
100
# File 'lib/copy_method/method_copier.rb', line 98

def from_module?
  looks_like_module? from
end

#method_sourceString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


69
70
71
# File 'lib/copy_method/method_copier.rb', line 69

def method_source
  definition.source
end

#originObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wrapper around #from



35
36
37
# File 'lib/copy_method/method_copier.rb', line 35

def origin
  from
end

#singleton_receiverObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Populated with the ‘receiver` for defining singleton methods.

In the method source, this would be something that looks like ‘def self.foo` where `foo` is the method name, and `self` is the receiver.

Since we’re copying with source, the presence of this determines where to define the new method on the target.



81
82
83
# File 'lib/copy_method/method_copier.rb', line 81

def singleton_receiver
  @_singleton_receiver ||= singleton? ? ( method_source[receiver_rgx, 2] || '' ) : ''
end

#singleton_receiver?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


85
86
87
# File 'lib/copy_method/method_copier.rb', line 85

def singleton_receiver?
  !singleton_receiver.empty?
end

#targetClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wrapper around #to.

If copying a singleton method, the possibility that it has a #singleton_receiver in its method body needs to be accounted for.

Returns:

  • (Class)


45
46
47
48
49
50
51
# File 'lib/copy_method/method_copier.rb', line 45

def target
  if singleton_target?
    singleton_receiver? ? to : to.singleton_class
  else
    to
  end
end

#to_module?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


102
103
104
# File 'lib/copy_method/method_copier.rb', line 102

def to_module?
  looks_like_module? to
end