Class: MethodDisabling::DisabledMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/method_disabling.rb

Overview

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 ClassMethods to disable and restore methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, method_name, message = nil) ⇒ DisabledMethod

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

Parameters:

  • klass (Module)

    The module or class whose method should be disabled.

  • method_name (Symbol, String)

    The name of the method to disable.

  • message (String) (defaults to: nil)

    The exception message to be shown when the method is called.



98
99
100
101
102
103
104
105
# 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

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



89
90
91
# File 'lib/method_disabling.rb', line 89

def klass
  @klass
end

#messageObject (readonly)

Returns the value of attribute message.



89
90
91
# File 'lib/method_disabling.rb', line 89

def message
  @message
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



89
90
91
# File 'lib/method_disabling.rb', line 89

def method_name
  @method_name
end

Instance Method Details

#disable!Object

Disables the method.



108
109
110
# File 'lib/method_disabling.rb', line 108

def disable!
  @disabled = true
end

#execute(object, *args, &block) ⇒ Object

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

Parameters:

  • object (Object)

    The “self” object of the method being called.

  • args (Array)

    The arguments that were passed to the method.

  • block (Proc)

    The block that was passed to the method.

Returns:

  • Whatever the original method returns.



135
136
137
138
139
140
141
# 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!Object

Restores the method.



113
114
115
# File 'lib/method_disabling.rb', line 113

def restore!
  @disabled = false
end

#to_procObject

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



118
119
120
121
122
123
124
125
# 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