Module: TypeToolkit::AbstractInstanceMethodReceiver

Defined in:
lib/type_toolkit/abstract_method_receiver.rb

Overview

This module is included on a class whose instances can be receivers of calls to abstract methods.

Since abstract methods are removed at runtime (see TypeToolkit::DSL#abstract), attempting to call an unimplemented abstract method would usually raise a NoMethodError. This module uses method_missing to raise AbstractMethodNotImplementedError instead. @requires_ancestor: Kernel

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

This #method_missing is hit when calling a potentially abstract method on an instance E.g. TheClass.new.maybe_abstract_method

(Symbol, ...) -> untyped



24
25
26
27
28
29
30
31
32
# File 'lib/type_toolkit/abstract_method_receiver.rb', line 24

def method_missing(method_name, ...)
  c = self.class #: as Class[top] & HasAbstractMethods

  if c.abstract_method_declared?(method_name)
    raise AbstractMethodNotImplementedError.new(method_name:)
  end

  super
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Signature:

  • (Symbol, ?bool) -> bool

Returns:

  • (Boolean)


35
36
37
# File 'lib/type_toolkit/abstract_method_receiver.rb', line 35

def respond_to_missing?(method_name, include_private = false)
  self.class.abstract_method_declared?(method_name) || super
end