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
Public Class Methods
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
Disables the method.
# File lib/method_disabling.rb, line 108 def disable! @disabled = true end
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
Restores the method.
# File lib/method_disabling.rb, line 113 def restore! @disabled = false end
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
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
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
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
Indicates whether or not the method is disabled.
@return [Boolean]
# File lib/method_disabling.rb, line 148 def disabled? @disabled end
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
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
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