Class: HoneyFormat::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/honey_format/registry.rb

Overview

Registry that holds value callers

Instance Method Summary collapse

Constructor Details

#initialize(default = {}) ⇒ Registry

Instantiate a caller registry

Parameters:

  • default (Hash) (defaults to: {})

    hash of defaults



8
9
10
11
12
# File 'lib/honey_format/registry.rb', line 8

def initialize(default = {})
  @callers = nil
  @default = default.dup
  reset!
end

Instance Method Details

#[](type) ⇒ Object

Returns the given type or raises error if type doesn’t exist

Parameters:

  • type (Symbol, String)

    the name of the type

Returns:

  • (Object)

    returns the caller

Raises:



68
69
70
# File 'lib/honey_format/registry.rb', line 68

def [](type)
  @callers.fetch(to_key(type)) { unknown_type_error!(type) }
end

#[]=(type, caller) ⇒ Object

Register a caller

Parameters:

  • type (Symbol, String)

    the name of the type

  • caller (#call)

    that responds to #call

Returns:

  • (Object)

    returns the caller

Raises:



54
55
56
57
58
59
60
61
62
# File 'lib/honey_format/registry.rb', line 54

def []=(type, caller)
  type = to_key(type)

  if type?(type)
    raise(Errors::TypeExistsError, "type '#{type}' already exists")
  end

  @callers[type] = caller
end

#call(value, type) ⇒ Object

Call value type

Parameters:

  • type (Symbol, String, #call)

    the name of the type

  • value (Object)

    to be converted



43
44
45
46
47
# File 'lib/honey_format/registry.rb', line 43

def call(value, type)
  return type.call(value) if type.respond_to?(:call)

  self[type].call(value)
end

#register(type, caller) ⇒ Registry

Register a caller

Parameters:

  • type (Symbol, String)

    the name of the type

  • caller (#call)

    that responds to #call

Returns:

Raises:



25
26
27
28
# File 'lib/honey_format/registry.rb', line 25

def register(type, caller)
  self[type] = caller
  self
end

#reset!Registry

Resets the caller registry to its default configuration

Returns:

  • (Registry)

    returns the caller registry



83
84
85
86
# File 'lib/honey_format/registry.rb', line 83

def reset!
  @callers = @default.dup
  self
end

#type?(type) ⇒ true, false

Returns true if the type exists, false otherwise

Parameters:

  • type (Symbol, String)

    the name of the type

Returns:

  • (true, false)

    true if type exists, false otherwise



75
76
77
78
79
# File 'lib/honey_format/registry.rb', line 75

def type?(type)
  return false unless keyable?(type)

  @callers.key?(to_key(type))
end

#typesArray<Symbol>

Returns list of registered types

Returns:

  • (Array<Symbol>)

    list of registered types



16
17
18
# File 'lib/honey_format/registry.rb', line 16

def types
  @callers.keys
end

#unregister(type) ⇒ Registry

Unregister a caller

Parameters:

  • type (Symbol, String)

    the name of the type

Returns:

Raises:



34
35
36
37
38
# File 'lib/honey_format/registry.rb', line 34

def unregister(type)
  unknown_type_error!(type) unless type?(type)
  @callers.delete(to_key(type))
  self
end