Class: T::Private::ClassUtils::ReplacedMethod
- Inherits:
-
Object
- Object
- T::Private::ClassUtils::ReplacedMethod
- Defined in:
- lib/types/private/class_utils.rb
Instance Method Summary collapse
- #bind(obj) ⇒ Object
-
#initialize(mod, old_method, new_method, overwritten, visibility) ⇒ ReplacedMethod
constructor
A new instance of ReplacedMethod.
- #restore ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(mod, old_method, new_method, overwritten, visibility) ⇒ ReplacedMethod
Returns a new instance of ReplacedMethod.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/types/private/class_utils.rb', line 8 def initialize(mod, old_method, new_method, overwritten, visibility) if old_method.name != new_method.name raise "Method names must match. old=#{old_method.name} new=#{new_method.name}" end @mod = mod @old_method = old_method @new_method = new_method @overwritten = overwritten @name = old_method.name @visibility = visibility @restored = false end |
Instance Method Details
#bind(obj) ⇒ Object
50 51 52 |
# File 'lib/types/private/class_utils.rb', line 50 def bind(obj) @old_method.bind(obj) end |
#restore ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/types/private/class_utils.rb', line 21 def restore # The check below would also catch this, but this makes the failure mode much clearer if @restored raise "Method '#{@name}' on '#{@mod}' was already restored" end if @mod.instance_method(@name) != @new_method raise "Trying to restore #{@mod}##{@name} but the method has changed since the call to replace_method" end @restored = true if @overwritten # The original method was overwritten. Overwrite again to restore it. T::Configuration.without_ruby_warnings do @mod.send(:define_method, @old_method.name, @old_method) end else # The original method was in an ancestor. Restore it by removing the overriding method. @mod.send(:remove_method, @old_method.name) end # Restore the visibility. Note that we need to do this even when we call remove_method # above, because the module may have set custom visibility for a method it inherited. @mod.send(@visibility, @old_method.name) nil end |
#to_s ⇒ Object
54 55 56 |
# File 'lib/types/private/class_utils.rb', line 54 def to_s @old_method.to_s end |