Class: Extant::Coercers::Hash

Inherits:
Base
  • Object
show all
Defined in:
lib/extant/coercers/hash.rb

Constant Summary

Constants inherited from Base

Base::UncoercedValue

Class Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#coerced?, coercer_name, #initialize

Constructor Details

This class inherits a constructor from Extant::Coercers::Base

Class Attribute Details

.key_typeObject

Returns the value of attribute key_type.



4
5
6
# File 'lib/extant/coercers/hash.rb', line 4

def key_type
  @key_type
end

.value_typeObject

Returns the value of attribute value_type.



4
5
6
# File 'lib/extant/coercers/hash.rb', line 4

def value_type
  @value_type
end

Instance Method Details

#coerceObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/extant/coercers/hash.rb', line 7

def coerce
  result = ::Hash[value]

  if key_type && value_type
    result = result.each_with_object({}) do |(k, v), h|
      h[coerce_key(k)] = coerce_value(v)
    end
  end

  self.coerced = true

  result
rescue ArgumentError
  UncoercedValue
end