Class: LedgerSync::Serialization::Type::DeserializerType

Inherits:
Type::Value
  • Object
show all
Defined in:
lib/ledger_sync/serialization/type/deserializer_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Type::ValueMixin

#assert_valid, #cast, included, #valid?

Constructor Details

#initialize(args = {}) ⇒ DeserializerType

Returns a new instance of DeserializerType.



9
10
11
12
13
# File 'lib/ledger_sync/serialization/type/deserializer_type.rb', line 9

def initialize(args = {})
  @deserializer = args.fetch(:deserializer)

  super()
end

Instance Attribute Details

#deserializerObject (readonly)

Returns the value of attribute deserializer.



7
8
9
# File 'lib/ledger_sync/serialization/type/deserializer_type.rb', line 7

def deserializer
  @deserializer
end

Instance Method Details

#cast_value(args = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ledger_sync/serialization/type/deserializer_type.rb', line 15

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

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

  return if value.nil?

  deserializer.new.deserialize(hash: value, resource: nested_resource)
end