Sha256: ec90cb44222b11ffffb0a7688eff99480a9c4473ed3a0a9262369cecea436c00

Contents?: true

Size: 1.48 KB

Versions: 48

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

class Module
  if RUBY_VERSION >= "2.3"
    # Marks the named method as intended to be redefined, if it exists.
    # Suppresses the Ruby method redefinition warning. Prefer
    # #redefine_method where possible.
    def silence_redefinition_of_method(method)
      if method_defined?(method) || private_method_defined?(method)
        # This suppresses the "method redefined" warning; the self-alias
        # looks odd, but means we don't need to generate a unique name
        alias_method method, method
      end
    end
  else
    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
  end

  # Replaces the existing method definition, if there is one, with the passed
  # block as its body.
  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.
  def redefine_singleton_method(method, &block)
    singleton_class.redefine_method(method, &block)
  end

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

Version data entries

48 entries across 48 versions & 7 rubygems

Version Path
activesupport-5.2.8.1 lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.8 lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.7.1 lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.7 lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.6.3 lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.6.2 lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.6.1 lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.6 lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.4.6 lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.5 lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.4.5 lib/active_support/core_ext/module/redefine_method.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.1/lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.4.4 lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.4.3 lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.4.2 lib/active_support/core_ext/module/redefine_method.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.4.1/lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.4.1 lib/active_support/core_ext/module/redefine_method.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.4 lib/active_support/core_ext/module/redefine_method.rb
activesupport-5.2.4.rc1 lib/active_support/core_ext/module/redefine_method.rb