Class: Tdc::ExtendedAttributes::InterpreterRegistry

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/tdc/extended_attributes/interpreter_registry.rb

Overview

Knows the class instances that interpret extended attribute values.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInterpreterRegistry

Returns a new instance of InterpreterRegistry.



13
14
15
# File 'lib/tdc/extended_attributes/interpreter_registry.rb', line 13

def initialize
  @interpreters = []
end

Class Method Details

.register(interpreter:) ⇒ Object



9
10
11
# File 'lib/tdc/extended_attributes/interpreter_registry.rb', line 9

def self.register(interpreter:)
  instance.register_interpreter(interpreter)
end

Instance Method Details

#clearObject



17
18
19
# File 'lib/tdc/extended_attributes/interpreter_registry.rb', line 17

def clear
  @interpreters = []
end

#interpretersObject



21
22
23
# File 'lib/tdc/extended_attributes/interpreter_registry.rb', line 21

def interpreters
  @interpreters.empty? ? [default_interpreter] : @interpreters
end

#register_interpreter(interpreter) ⇒ Object

Raises:



25
26
27
28
29
30
31
32
33
34
# File 'lib/tdc/extended_attributes/interpreter_registry.rb', line 25

def register_interpreter(interpreter)
  raise Tdc::FatalError, <<~MSG.chomp unless interpreter.is_a?(Tdc::ExtendedAttributes::InterpreterBase)
    Cannot register an interpreter unless it inherits from Tdc::ExtendedAttributes::InterpreterBase
  MSG

  # Avoid registering the same class of interpreter a second time.
  return if @interpreters.map(&:class).include?(interpreter.class)

  @interpreters << interpreter
end