Class: ValueSemantics::HashCoercer
- Inherits:
-
Object
- Object
- ValueSemantics::HashCoercer
- Defined in:
- lib/value_semantics/hash_coercer.rb
Instance Attribute Summary collapse
-
#key_coercer ⇒ Object
readonly
Returns the value of attribute key_coercer.
-
#value_coercer ⇒ Object
readonly
Returns the value of attribute value_coercer.
Instance Method Summary collapse
- #call(obj) ⇒ Object
-
#initialize(key_coercer:, value_coercer:) ⇒ HashCoercer
constructor
A new instance of HashCoercer.
Constructor Details
#initialize(key_coercer:, value_coercer:) ⇒ HashCoercer
Returns a new instance of HashCoercer.
5 6 7 8 |
# File 'lib/value_semantics/hash_coercer.rb', line 5 def initialize(key_coercer:, value_coercer:) @key_coercer, @value_coercer = key_coercer, value_coercer freeze end |
Instance Attribute Details
#key_coercer ⇒ Object (readonly)
Returns the value of attribute key_coercer.
3 4 5 |
# File 'lib/value_semantics/hash_coercer.rb', line 3 def key_coercer @key_coercer end |
#value_coercer ⇒ Object (readonly)
Returns the value of attribute value_coercer.
3 4 5 |
# File 'lib/value_semantics/hash_coercer.rb', line 3 def value_coercer @value_coercer end |
Instance Method Details
#call(obj) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/value_semantics/hash_coercer.rb', line 10 def call(obj) hash = coerce_to_hash(obj) return obj unless hash {}.tap do |result| hash.each do |key, value| r_key = key_coercer.(key) r_value = value_coercer.(value) result[r_key] = r_value end end end |