Class: LedgerSync::Serialization::Type::DeserializerReferencesManyType

Inherits:
DeserializerType show all
Defined in:
lib/ledger_sync/serialization/type/deserializer_references_many_type.rb

Instance Attribute Summary

Attributes inherited from DeserializerType

#deserializer

Instance Method Summary collapse

Methods inherited from DeserializerType

#initialize

Methods included from Type::ValueMixin

#assert_valid, #cast, included, #valid?

Constructor Details

This class inherits a constructor from LedgerSync::Serialization::Type::DeserializerType

Instance Method Details

#cast_value(args = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ledger_sync/serialization/type/deserializer_references_many_type.rb', line 9

def cast_value(args = {})
  deserializer_attribute = args.fetch(:deserializer_attribute)
  value                  = args.fetch(:value)
  resource               = args.fetch(:resource)

  return if value.nil?

  first_dot = deserializer_attribute.resource_attribute_dot_parts.first.to_sym
  nested_resource = resource.class.resource_attributes[first_dot].type.resource_class.new

  value.map do |one_value|
    deserializer.new.deserialize(hash: one_value, resource: nested_resource)
  end
end