module MethodDisabling::ClassMethods
Provides class-level macros for managing disabled methods.
@example Disabling an instance method
class Foo def bar 42 end end Foo.new.bar # => 42 Foo.disable_method :bar Foo.new.bar # => NoMethodError: Foo#bar is disabled Foo.restore_method :bar Foo.new.bar # => 42
@example Disabling a class method
class Foo def self.bar 42 end end Foo.bar # => 42 Foo.disable_class_method :bar Foo.bar # => NoMethodError: #<Class:Foo>#bar is disabled Foo.restore_class_method :bar Foo.bar # => 42
Public Instance Methods
Disables a class method.
@param [Symbol,String] method_name The name of the method to disable. @param [String] message An error message. Defaults to “Class#method is disabled”.
# File lib/method_disabling.rb, line 66 def disable_class_method(method_name, message = nil) (class << self; self; end).disable_method(method_name, message) end
Disables an instance method.
@param [Symbol,String] method_name The name of the method to disable. @param [String] message An error message. Defaults to “Class#method is disabled”.
# File lib/method_disabling.rb, line 42 def disable_method(method_name, message = nil) disabled_methods[method_name] ||= DisabledMethod.new(self, method_name, message) disabled_methods[method_name].disable! end
Restores a previously disabled class method.
@param [Symbol,String] method_name The name of the method to restore.
# File lib/method_disabling.rb, line 73 def restore_class_method(method_name) (class << self; self; end).restore_method(method_name) end
Restores a previously disabled instance method.
@param [Symbol,String] method_name The name of the method to restore.
# File lib/method_disabling.rb, line 50 def restore_method(method_name) disabled_methods[method_name].restore! end
Private Instance Methods
A collection of the methods that have been disabled.
@return [Hash]
# File lib/method_disabling.rb, line 57 def disabled_methods @disabled_methods ||= {} end