Sha256: 13c4fa8770bd7c3e3c7628315621b3fad83e9a4d94cd3cdb75d85b2d0224ae3b

Contents?: true

Size: 886 Bytes

Versions: 4

Compression:

Stored size: 886 Bytes

Contents

# frozen_string_literal: true

module LedgerSync
  module Serialization
    module Type
      class DeserializerType < LedgerSync::Type::Value
        attr_reader :deserializer

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

        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
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ledger_sync-1.4.4 lib/ledger_sync/serialization/type/deserializer_type.rb
ledger_sync-1.4.2 lib/ledger_sync/serialization/type/deserializer_type.rb
ledger_sync-1.4.1 lib/ledger_sync/serialization/type/deserializer_type.rb
ledger_sync-1.4.0 lib/ledger_sync/serialization/type/deserializer_type.rb