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.



15
16
17
# File 'lib/tdc/extended_attributes/interpreter_registry.rb', line 15

def initialize
  @interpreters = []
end

Class Method Details

.register(interpreter:) ⇒ Object



11
12
13
# File 'lib/tdc/extended_attributes/interpreter_registry.rb', line 11

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

Instance Method Details

#clearObject



19
20
21
# File 'lib/tdc/extended_attributes/interpreter_registry.rb', line 19

def clear
  @interpreters = []
end

#interpretersObject



23
24
25
# File 'lib/tdc/extended_attributes/interpreter_registry.rb', line 23

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

#register_interpreter(interpreter) ⇒ Object

Raises:



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

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