Sha256: 87e6238f7aefa6c885cd18d45a0f4c8720afd6549757d79d3141e7d4f3b60bf5

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

# :nodoc:

unless defined?(Rails) || defined?(ActiveSupport)
  # See {Module}
  # @api private
  class Module
    # @api private
    # :nodoc:
    def silence_redefinition_of_method(method)
      if method_defined?(method) || private_method_defined?(method)
        alias_method :__rails_redefine, method
        remove_method :__rails_redefine
      end
    end

    # Replaces the existing method definition, if there is one, with the passed
    # block as its body.
    # @api private
    # :nodoc:
    def redefine_method(method, &block)
      visibility = method_visibility(method)
      silence_redefinition_of_method(method)
      define_method(method, &block)
      send(visibility, method)
    end

    # Replaces the existing singleton method definition, if there is one, with
    # the passed block as its body.
    # @api private
    # :nodoc:
    def redefine_singleton_method(method, &block)
      singleton_class.redefine_method(method, &block)
    end

    # @api private
    # :nodoc:
    def method_visibility(method) # :nodoc:
      case
      when private_method_defined?(method)
        :private
      when protected_method_defined?(method)
        :protected
      else
        :public
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stub_requests-0.1.5 lib/stub_requests/core_ext/module/redefine_method.rb
stub_requests-0.1.4 lib/stub_requests/core_ext/module/redefine_method.rb