Class: ValueSemantics::ValueObjectCoercer
- Inherits:
-
Object
- Object
- ValueSemantics::ValueObjectCoercer
- Defined in:
- lib/value_semantics/value_object_coercer.rb
Overview
A coercer for converting hashes into instances of value classes
The coercer will coerce hash-like values into an instance of the value class, using the hash for attribute values. It will return non-hash-like values unchanged. It will also return hash-like values unchanged if they do not contain all required attributes of the value class.
Instance Attribute Summary collapse
-
#value_class ⇒ Object
readonly
Returns the value of attribute value_class.
Instance Method Summary collapse
- #call(obj) ⇒ Object
-
#initialize(value_class) ⇒ ValueObjectCoercer
constructor
A new instance of ValueObjectCoercer.
Constructor Details
#initialize(value_class) ⇒ ValueObjectCoercer
Returns a new instance of ValueObjectCoercer.
13 14 15 |
# File 'lib/value_semantics/value_object_coercer.rb', line 13 def initialize(value_class) @value_class = value_class end |
Instance Attribute Details
#value_class ⇒ Object (readonly)
Returns the value of attribute value_class.
11 12 13 |
# File 'lib/value_semantics/value_object_coercer.rb', line 11 def value_class @value_class end |
Instance Method Details
#call(obj) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/value_semantics/value_object_coercer.rb', line 17 def call(obj) attrs = coerce_to_attr_hash(obj) if attrs value_class.new(attrs) else obj end end |