Class: VCDry::Types::TypeRegistry
- Inherits:
-
Object
- Object
- VCDry::Types::TypeRegistry
- Defined in:
- lib/vcdry/types.rb
Instance Method Summary collapse
- #[](name) ⇒ Object
- #add_type(name, method) ⇒ Object
-
#initialize ⇒ TypeRegistry
constructor
A new instance of TypeRegistry.
Constructor Details
#initialize ⇒ TypeRegistry
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
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 |