class Extant::Coercers::Hash

Attributes

key_type[RW]
value_type[RW]

Public Instance Methods

coerce() click to toggle source
# 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

Private Instance Methods

coerce_key(v) click to toggle source
# File lib/extant/coercers/hash.rb, line 25
def coerce_key(v)
  coercer = key_type.new(v)
  result = coercer.coerce
  raise ArgumentError unless coercer.coerced?
  result
end
coerce_value(v) click to toggle source
# File lib/extant/coercers/hash.rb, line 32
def coerce_value(v)
  coercer = value_type.new(v)
  result = coercer.coerce
  raise ArgumentError unless coercer.coerced?
  result
end
key_type() click to toggle source
# File lib/extant/coercers/hash.rb, line 39
def key_type
  self.class.key_type
end
value_type() click to toggle source
# File lib/extant/coercers/hash.rb, line 43
def value_type
  self.class.value_type
end