Exception: NRSER::AbstractMethodError

Inherits:
NotImplementedError
  • Object
show all
Defined in:
lib/nrser/errors.rb

Overview

Extension of Ruby’s NotImplementedError to provide a useful message and convenient constructor for abstract methods.

Examples:


def f
  raise NRSER::AbstractMethodError.new( self, __method__ )

Instance Method Summary collapse

Constructor Details

#initialize(instance, method_name) ⇒ AbstractMethodError

Construct a new ‘AbstractMethodError`.

Parameters:

  • instance (Object)

    Instance that invoked the abstract method.

  • method_name (Symbol | String)

    Name of abstract method.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nrser/errors.rb', line 27

def initialize instance, method_name
  @instance = instance
  @method_name = method_name
  @method = instance.method @method_name
  
  message = if @method.owner == instance.class
    NRSER.dedent <<-END
      Method #{ @method.owner.name }##{ @method_name } is abstract, meaning 
      #{ @method.owner.name } is an abstract class and the invoking 
      instance #{ @instance } should NOT have been constructed.
    END
  else
    NRSER.squish <<-END
      Method #{ @method.owner.name }##{ @method_name } is abstract and 
      has not been implemented in invoking class #{ @instance.class }.
      
      If you *are* developing the invoking class #{ @instance.class } it
      (or a parent class between it and #{ @method.owner.name }) must 
      implement ##{ @method_name }.
      
      If you *are not* developing #{ @instance.class } it should be treated
      as an abstract base class and should NOT be constructed. You need to
      find a subclass of #{ @instance.class } to instantiate or write 
      your own.
    END
  end
  
  super message
end