Sha256: 6fbe78ae9b0ff1a3bddd6737f7420c840a040c0c7269d501511a9b985d81a4cd

Contents?: true

Size: 1.66 KB

Versions: 21

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true
# copied from activesupport/core_ext from Rails, MIT license
# https://github.com/rails/rails/tree/a713fdae4eb4f7ccd34932edc61561a96b8d9f35/activesupport/lib/active_support/core_ext/module

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

21 entries across 21 versions & 1 rubygems

Version Path
ruby-dbus-0.24.0 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.23.1 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.23.0.beta2 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.23.0.beta1 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.22.1 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.22.0 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.21.0 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.20.0 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.19.0 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.18.1 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.18.0.beta8 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.18.0.beta7 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.18.0.beta6 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.18.0.beta5 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.18.0.beta4 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.18.0.beta3 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.18.0.beta2 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.18.0.beta1 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.17.0 lib/dbus/core_ext/module/redefine_method.rb
ruby-dbus-0.16.0 lib/dbus/core_ext/module/redefine_method.rb