Class: LedgerSync::Serialization::DeserializerAttribute
- Defined in:
- lib/ledger_sync/serialization/deserializer_attribute.rb
Instance Attribute Summary
Attributes inherited from Attribute
#block, #hash_attribute, #resource_attribute, #resource_class, #type
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ DeserializerAttribute
constructor
A new instance of DeserializerAttribute.
- #value_from_hash(hash:, resource:) ⇒ Object
Methods inherited from Attribute
#block_value_for, #hash_attribute_dot_parts, #reference?, #references_many?, #references_one?, #resource_attribute_dot_parts
Constructor Details
#initialize(args = {}) ⇒ DeserializerAttribute
Returns a new instance of DeserializerAttribute.
8 9 10 11 12 13 |
# File 'lib/ledger_sync/serialization/deserializer_attribute.rb', line 8 def initialize(args = {}) super raise 'Missing resource_attribute' if resource_attribute.blank? raise 'block and hash_attribute cannot both be present' unless block.nil? || hash_attribute.nil? end |
Instance Method Details
#value_from_hash(hash:, resource:) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ledger_sync/serialization/deserializer_attribute.rb', line 15 def value_from_hash(hash:, resource:) value = hash.dig(*hash_attribute.split('.')) value = type.convert(value: value) return value if resource_attribute_dot_parts.count <= 1 nested_resource = resource.send(resource_attribute_dot_parts.first) nested_resource ||= resource_attribute_class(resource: resource).new build_resource_value_from_nested_attributes( nested_resource, value, resource_attribute_dot_parts[1..-1] ) end |