module T::Private::ClassUtils

Cut down version of Chalk::Tools::ClassUtils with only :replace_method functionality. Extracted to a separate namespace so the type system can be used standalone.

Public Class Methods

replace_method(mod, name, &blk) click to toggle source

Replaces a method, either by overwriting it (if it is defined directly on `mod`) or by overriding it (if it is defined by one of mod's ancestors). Returns a ReplacedMethod instance on which you can call `bind(…).call(…)` to call the original method, or `restore` to restore the original method (by overwriting or removing the override).

# File lib/types/private/class_utils.rb, line 76
def self.replace_method(mod, name, &blk)
  original_method = mod.instance_method(name)
  original_visibility = visibility_method_name(mod, name)
  original_owner = original_method.owner

  mod.ancestors.each do |ancestor|
    break if ancestor == mod
    if ancestor == original_owner
      # If we get here, that means the method we're trying to replace exists on a *prepended*
      # mixin, which means in order to supersede it, we'd need to create a method on a new
      # module that we'd prepend before `ancestor`. The problem with that approach is there'd
      # be no way to remove that new module after prepending it, so we'd be left with these
      # empty anonymous modules in the ancestor chain after calling `restore`.
      #
      # That's not necessarily a deal breaker, but for now, we're keeping it as unsupported.
      raise "You're trying to replace `#{name}` on `#{mod}`, but that method exists in a " \
            "prepended module (#{ancestor}), which we don't currently support. Talk to " \
            "#dev-productivity for help."
    end
  end

  overwritten = original_owner == mod
  T::Configuration.without_ruby_warnings do
    T::Private::DeclState.current.without_on_method_added do
      mod.send(:define_method, name, &blk)
      if blk.arity < 0 && mod.respond_to?(:ruby2_keywords, true)
        mod.send(:ruby2_keywords, name)
      end
    end
  end
  mod.send(original_visibility, name)
  new_method = mod.instance_method(name)

  ReplacedMethod.new(mod, original_method, new_method, overwritten, original_visibility)
end

Private Class Methods

visibility_method_name(mod, name) click to toggle source

`name` must be an instance method (for class methods, pass in mod.singleton_class)

# File lib/types/private/class_utils.rb, line 60
                     def self.visibility_method_name(mod, name)
  if mod.public_method_defined?(name)
    :public
  elsif mod.protected_method_defined?(name)
    :protected
  elsif mod.private_method_defined?(name)
    :private
  else
    raise NameError.new("undefined method `#{name}` for `#{mod}`")
  end
end