Class: CopyMethod::MethodCopier Private
- Inherits:
-
Object
- Object
- CopyMethod::MethodCopier
- 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.
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
- #from ⇒ Object readonly private
- #method_name ⇒ Object readonly private
- #remove ⇒ Object (also: #remove?) readonly private
- #singleton ⇒ Object (also: #singleton?) readonly private
- #singleton_target ⇒ Object (also: #singleton_target?) readonly private
- #to ⇒ Object readonly private
Instance Method Summary collapse
- #class_receiver? ⇒ Boolean private
-
#convert_to_instance_method? ⇒ Boolean
private
This checks for the condition where we want to copy a ‘Class`’s singleton method to a ‘Module`’s instance methods.
-
#copy! ⇒ Symbol
private
Where the magic happens.
- #corrected_source ⇒ String private
- #definition ⇒ Object private
- #from_module? ⇒ Boolean private
-
#initialize(name:, from:, to:, singleton: false, remove: false, singleton_target: nil) ⇒ MethodCopier
constructor
private
A new instance of MethodCopier.
- #method_source ⇒ String private
-
#origin ⇒ Object
private
Wrapper around #from.
-
#singleton_receiver ⇒ Object
private
Populated with the ‘receiver` for defining singleton methods.
- #singleton_receiver? ⇒ Boolean private
-
#target ⇒ Class
private
Wrapper around #to.
- #to_module? ⇒ Boolean private
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
#from ⇒ Object (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_name ⇒ Object (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 |
#remove ⇒ Object (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 |
#singleton ⇒ Object (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_target ⇒ Object (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 |
#to ⇒ Object (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.
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`.
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.
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_source ⇒ String
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.
94 95 96 |
# File 'lib/copy_method/method_copier.rb', line 94 def corrected_source correct_source_receiver method_source end |
#definition ⇒ Object
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.
98 99 100 |
# File 'lib/copy_method/method_copier.rb', line 98 def from_module? looks_like_module? from end |
#method_source ⇒ String
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.
69 70 71 |
# File 'lib/copy_method/method_copier.rb', line 69 def method_source definition.source end |
#origin ⇒ Object
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_receiver ⇒ Object
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.
85 86 87 |
# File 'lib/copy_method/method_copier.rb', line 85 def singleton_receiver? !singleton_receiver.empty? end |
#target ⇒ Class
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.
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.
102 103 104 |
# File 'lib/copy_method/method_copier.rb', line 102 def to_module? looks_like_module? to end |