Class: Strict::Coercers::Hash

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_coercer = nil, value_coercer = nil) ⇒ Hash

Returns a new instance of Hash.



8
9
10
11
# File 'lib/strict/coercers/hash.rb', line 8

def initialize(key_coercer = nil, value_coercer = nil)
  @key_coercer = key_coercer
  @value_coercer = value_coercer
end

Instance Attribute Details

#key_coercerObject (readonly)

Returns the value of attribute key_coercer.



6
7
8
# File 'lib/strict/coercers/hash.rb', line 6

def key_coercer
  @key_coercer
end

#value_coercerObject (readonly)

Returns the value of attribute value_coercer.



6
7
8
# File 'lib/strict/coercers/hash.rb', line 6

def value_coercer
  @value_coercer
end

Instance Method Details

#call(value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/strict/coercers/hash.rb', line 13

def call(value)
  return value if value.nil? || !value.respond_to?(:to_h)

  if key_coercer && value_coercer
    coerce_keys_and_values(value.to_h)
  elsif key_coercer
    coerce_keys(value.to_h)
  elsif value_coercer
    coerce_values(value.to_h)
  else
    value.to_h
  end
end