Class: MethodDisabling::DisabledMethod
- Inherits:
-
Object
- Object
- MethodDisabling::DisabledMethod
- 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
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
Instance Method Summary collapse
-
#disable! ⇒ Object
Disables the method.
-
#execute(object, *args, &block) ⇒ Object
The replacement for the original method.
-
#initialize(klass, method_name, message = nil) ⇒ DisabledMethod
constructor
Disables a instance method.
-
#restore! ⇒ Object
Restores the method.
-
#to_proc ⇒ Object
Returns a Proc that acts as a replacement for the disabled method.
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.
98 99 100 101 102 103 104 105 |
# File 'lib/method_disabling.rb', line 98 def initialize(klass, method_name, = nil) @klass = klass @method_name = method_name @message = || "#{klass.inspect}##{method_name} is disabled" alias_method! disable! end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
89 90 91 |
# File 'lib/method_disabling.rb', line 89 def klass @klass end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
89 90 91 |
# File 'lib/method_disabling.rb', line 89 def @message end |
#method_name ⇒ Object (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.
135 136 137 138 139 140 141 |
# File 'lib/method_disabling.rb', line 135 def execute(object, *args, &block) if disabled? raise NoMethodError, 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_proc ⇒ Object
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 |