Exception: NRSER::AbstractMethodError

Inherits:
NotImplementedError
  • Object
show all
Includes:
Log::Mixin, NicerError
Defined in:
lib/nrser/errors/abstract_method_error.rb

Overview

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

This is a NicerError.

Examples:


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

Constant Summary

Constants included from NicerError

NicerError::DEFAULT_COLUMN_WIDTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log::Mixin

included, #logger, #logger=

Methods included from NicerError

#add_extended_message?, column_width, #context_section, #details_section, #extended_message, #format_message, #format_message_segment, included, #to_s

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.



65
66
67
68
69
70
71
# File 'lib/nrser/errors/abstract_method_error.rb', line 65

def initialize instance, method_name
  @instance = instance
  @method_name = method_name
  @method_instance = instance.method @method_name
  
  super()
end

Instance Attribute Details

#instanceattr_type (readonly)

TODO document ‘instance` attribute.

Returns:

  • (attr_type)


53
54
55
# File 'lib/nrser/errors/abstract_method_error.rb', line 53

def instance
  @instance
end

#method_instanceMethod (readonly)

TODO document ‘method_instance` attribute.

Returns:



45
46
47
# File 'lib/nrser/errors/abstract_method_error.rb', line 45

def method_instance
  @method_instance
end

#method_nameSymbol (readonly)

The abstract method’s name that was called¹.

> ¹ I mean, that’s what it should be, it’s really just what was passed > as the ‘method_name:` keyword to #initialize.

Returns:



38
39
40
# File 'lib/nrser/errors/abstract_method_error.rb', line 38

def method_name
  @method_name
end

Instance Method Details

#contextObject



113
114
115
116
117
118
# File 'lib/nrser/errors/abstract_method_error.rb', line 113

def context
  {
    instance: instance,
    method_name: method_name,
  }
end

#default_messageObject



121
122
123
# File 'lib/nrser/errors/abstract_method_error.rb', line 121

def default_message
  "Method ##{ method_name.to_s } is abstract"
end

#detailsObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/nrser/errors/abstract_method_error.rb', line 126

def details
  @details ||= if method_owner == instance.class
    <<~END
      Method #{ method_full_name } is abstract, meaning
      #{ method_owner_name } is an abstract class and the invoking
      instance #{ instance } should NOT have been constructed.
    END
  else
    <<~END
      Method #{ method_full_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.to_s }.
      
      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
end

#method_full_nameObject



106
107
108
109
110
# File 'lib/nrser/errors/abstract_method_error.rb', line 106

def method_full_name
  lazy_var :@method_full_name do
    "#{ method_owner_name }##{ method_name.to_s }"
  end
end

#method_ownerObject



88
89
90
91
92
# File 'lib/nrser/errors/abstract_method_error.rb', line 88

def method_owner
  lazy_var :@method_owner do
    method_instance && method_instance.owner
  end
end

#method_owner_nameObject



95
96
97
98
99
100
101
102
103
# File 'lib/nrser/errors/abstract_method_error.rb', line 95

def method_owner_name
  lazy_var :@method_owner_name do
    if method_owner
      method_owner.safe_name
    else
      '???'
    end
  end
end