Class: VCDry::Types::TypeRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/vcdry/types.rb

Instance Method Summary collapse

Constructor Details

#initializeTypeRegistry

Returns a new instance of TypeRegistry.



16
17
18
19
20
21
22
23
24
25
# File 'lib/vcdry/types.rb', line 16

def initialize
  @types = {
    boolean: ->(value) { !!value },
    datetime: ->(value) { value.to_datetime },
    hash: ->(value) { value.to_h },
    integer: ->(value) { value.to_i },
    string: ->(value) { value.to_s },
    symbol: ->(value) { value.to_s.to_sym }
  }
end

Instance Method Details

#[](name) ⇒ Object



33
34
35
36
37
# File 'lib/vcdry/types.rb', line 33

def [](name)
  @types.fetch(name)
rescue KeyError
  raise UnknownTypeError.new(name)
end

#add_type(name, method) ⇒ Object

Raises:

  • (TypeError)


27
28
29
30
31
# File 'lib/vcdry/types.rb', line 27

def add_type(name, method)
  raise TypeError, "method must respond to #call" unless method.respond_to?(:call)

  @types[name] = method
end