Class: LedgerSync::Serialization::DeserializerAttribute
- Defined in:
- lib/ledger_sync/serialization/deserializer_attribute.rb
Instance Attribute Summary
Attributes inherited from Attribute
#block, #hash_attribute, #if_method, #resource_attribute, #resource_class, #type
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ DeserializerAttribute
constructor
A new instance of DeserializerAttribute.
- #references_many? ⇒ Boolean
- #references_one? ⇒ Boolean
- #value_from_hash(hash:, resource:) ⇒ Object
Methods inherited from Attribute
#block_value_for, #hash_attribute_dot_parts, #reference?, #resource_attribute_dot_parts
Constructor Details
#initialize(args = {}) ⇒ DeserializerAttribute
Returns a new instance of DeserializerAttribute.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/ledger_sync/serialization/deserializer_attribute.rb', line 8 def initialize(args = {}) super raise 'Missing resource_attribute' if resource_attribute.blank? if block.present? raise 'block and hash_attribute cannot both be present' if hash_attribute.present? else @hash_attribute ||= resource_attribute end end |
Instance Method Details
#references_many? ⇒ Boolean
20 21 22 |
# File 'lib/ledger_sync/serialization/deserializer_attribute.rb', line 20 def references_many? type.is_a?(Type::DeserializerReferencesManyType) end |
#references_one? ⇒ Boolean
24 25 26 |
# File 'lib/ledger_sync/serialization/deserializer_attribute.rb', line 24 def references_one? type.is_a?(Type::DeserializerReferencesOneType) end |
#value_from_hash(hash:, resource:) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ledger_sync/serialization/deserializer_attribute.rb', line 28 def value_from_hash(hash:, resource:) return block_value_for(hash: hash) if block.present? value = hash.dig(*hash_attribute.split('.')) if type.is_a?(Type::DeserializerType) type.cast(deserializer_attribute: self, resource: resource, value: value) else value = type.cast(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 end |