class MethodDisabling::DisabledMethod

A DisabledMethod is an existing class or instance method that has been disabled. The method can be disabled and restored as necessary. When the method is disabled, calling the method will raise a NoMethodError, optionally with a custom message. When the method is restored, the method will behave as normal.

Although this class could be used directly, the intention is that you would use the methods in {MethodDisabling::ClassMethods} to disable and restore methods.

Attributes

klass[R]
message[R]
method_name[R]

Public Class Methods

new(klass, method_name, message = nil) click to toggle source

Disables a instance method. To disable a class method, pass the class’s singleton class as the first argument.

@param [Module] klass The module or class whose method should be disabled. @param [Symbol,String] method_name The name of the method to disable. @param [String] message The exception message to be shown when the method is

called.
# File lib/method_disabling.rb, line 98
def initialize(klass, method_name, message = nil)
  @klass       = klass
  @method_name = method_name
  @message     = message || "#{klass.inspect}##{method_name} is disabled"

  alias_method!
  disable!
end

Public Instance Methods

disable!() click to toggle source

Disables the method.

# File lib/method_disabling.rb, line 108
def disable!
  @disabled = true
end
execute(object, *args, &block) click to toggle source

The replacement for the original method. It will raise a NoMethodError if the method is disabled. Otherwise, it will execute the original method.

@param [Object] object The “self” object of the method being called. @param [Array] args The arguments that were passed to the method. @param [Proc] block The block that was passed to the method.

@return Whatever the original method returns.

# File lib/method_disabling.rb, line 135
def execute(object, *args, &block)
  if disabled?
    raise NoMethodError, message
  else
    object.send(aliased_name, *args, &block)
  end
end
restore!() click to toggle source

Restores the method.

# File lib/method_disabling.rb, line 113
def restore!
  @disabled = false
end
to_proc() click to toggle source

Returns a Proc that acts as a replacement for the disabled method.

# File lib/method_disabling.rb, line 118
def to_proc
  disabled_method = self

  # This proc will be evaluated with "self" set to the original object.
  Proc.new do |*args, &block|
    disabled_method.execute(self, *args, &block)
  end
end

Private Instance Methods

alias_method!() click to toggle source

Replaces the original implementation of the method with an implementation that allows disabling.

# File lib/method_disabling.rb, line 154
def alias_method!
  klass.send(:define_method, replacement_name, &self)
  klass.send(:alias_method, aliased_name, method_name)
  klass.send(:alias_method, method_name, replacement_name)
end
aliased_name() click to toggle source

The aliased name of the original method.

@return [String]

# File lib/method_disabling.rb, line 170
def aliased_name
  "#{base_method_name}_without_disable#{method_suffix}"
end
base_method_name() click to toggle source

The original method name with any suffix (“!” or “?”) removed.

@return [String]

# File lib/method_disabling.rb, line 177
def base_method_name
  method_name_parts[:base_name]
end
disabled?() click to toggle source

Indicates whether or not the method is disabled.

@return [Boolean]

# File lib/method_disabling.rb, line 148
def disabled?
  @disabled
end
method_name_parts() click to toggle source

The original method name broken into parts. The returned value can is a hash with the keys ‘:base_name` and `:suffix`, which return the original method name’s base name and suffix, respectively.

@return [Hash]

# File lib/method_disabling.rb, line 193
def method_name_parts
  @parts ||= begin
    base_name, suffix = method_name.to_s.sub(/([?!=]?)$/, ""), $1
    { :base_name => base_name, :suffix => suffix }
  end
end
method_suffix() click to toggle source

The original method name’s suffix (“!” or “?”), if any.

@return [String]

# File lib/method_disabling.rb, line 184
def method_suffix
  method_name_parts[:suffix]
end
replacement_name() click to toggle source

The name of the replacement method.

@return [String]

# File lib/method_disabling.rb, line 163
def replacement_name
  "#{base_method_name}_with_disable#{method_suffix}"
end