Module: MethodDisabling::ClassMethods

Defined in:
lib/method_disabling.rb

Overview

Provides class-level macros for managing disabled methods.

Examples:

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

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

Instance Method Summary collapse

Instance Method Details

#disable_class_method(method_name, message = nil) ⇒ Object

Disables a class method.

Parameters:

  • method_name (Symbol, String)

    The name of the method to disable.

  • message (String) (defaults to: nil)

    An error message. Defaults to “Class#method is disabled”.



66
67
68
# 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

#disable_method(method_name, message = nil) ⇒ Object

Disables an instance method.

Parameters:

  • method_name (Symbol, String)

    The name of the method to disable.

  • message (String) (defaults to: nil)

    An error message. Defaults to “Class#method is disabled”.



42
43
44
45
# 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

#restore_class_method(method_name) ⇒ Object

Restores a previously disabled class method.

Parameters:

  • method_name (Symbol, String)

    The name of the method to restore.



73
74
75
# File 'lib/method_disabling.rb', line 73

def restore_class_method(method_name)
  (class << self; self; end).restore_method(method_name)
end

#restore_method(method_name) ⇒ Object

Restores a previously disabled instance method.

Parameters:

  • method_name (Symbol, String)

    The name of the method to restore.



50
51
52
# File 'lib/method_disabling.rb', line 50

def restore_method(method_name)
  disabled_methods[method_name].restore!
end