Class: ValueSemantics::ValueObjectCoercer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_classObject (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