Module: AbstractMethod

Defined in:
lib/regex/abstract_method.rb

Overview

Mix-in module. Provides the method 'abstract_method' that raises an exception with an appropriate message when called.

Instance Method Summary collapse

Instance Method Details

#abstract_methodObject

Call this method in the body of your abstract methods. Example: require 'AbstractMethod' class SomeClass include AbstractMethod # To add the behaviour from the mix-in module AbstractMethod ... Consider that SomeClass has an abstract method called 'some_method'

def some_method() abstract_method end

Raises:

  • (NotImplementedError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/regex/abstract_method.rb', line 16

def abstract_method
  # Determine the short class name of self
  className = self.class.name.split(/::/).last

  # Retrieve the top text line of the call stack
  top_line = caller(1..1)

  # Extract the calling method name
  callerNameInQuotes = top_line.scan(/`.+?$/).first
  callerName = callerNameInQuotes.gsub(/`|'/, '') # Remove enclosing quotes

  # Build the error message
  prefix = "The method #{className}##{callerName} is abstract."
  suffix = " It should be implemented in subclasses of #{className}."
  error_message = prefix + suffix
  raise NotImplementedError, error_message
end