Module: TrailerVote::Api::TypeRegistry

Defined in:
lib/trailer_vote/api/type_registry.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.registryObject

Returns the value of attribute registry.



12
13
14
# File 'lib/trailer_vote/api/type_registry.rb', line 12

def registry
  @registry
end

.registry_shortcutsObject

Returns the value of attribute registry_shortcuts.



12
13
14
# File 'lib/trailer_vote/api/type_registry.rb', line 12

def registry_shortcuts
  @registry_shortcuts
end

Class Method Details

.[](key) ⇒ Object



20
21
22
23
24
# File 'lib/trailer_vote/api/type_registry.rb', line 20

def [](key)
  registry.fetch(key) { registry.fetch(registry_shortcuts.fetch(key)) }
rescue KeyError
  raise UnknownMediaType, format('Unknown media type %<type>s', type: key)
end

.[]=(key, value) ⇒ Object



26
27
28
# File 'lib/trailer_vote/api/type_registry.rb', line 26

def []=(key, value)
  registry[key] = value
end

.register(registerable) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/trailer_vote/api/type_registry.rb', line 34

def register(registerable)
  mime_type = registerable.to_s

  self[mime_type] = AdapterFor(registerable)
  shortcut(mime_type, registerable.to_s)

  registerable.available_validations.each do |alias_mime_type|
    shortcut(mime_type, alias_mime_type)
  end
end

.shortcut(key, alias_name) ⇒ Object



30
31
32
# File 'lib/trailer_vote/api/type_registry.rb', line 30

def shortcut(key, alias_name)
  registry_shortcuts[alias_name] = key
end