Sha256: 76681e59921314e886756a2d14c602c25db13b3ff172527c0d380fa4bcef61c3

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module LedgerSync
  module Adaptors
    module QuickBooksOnline
      module LedgerSerializerType
        class EntityReferenceType < Adaptors::LedgerSerializerType::ValueType
          def convert_from_ledger(value:)
            return if value.nil?

            raise "Unknown value type.  Hash expected.  Given: #{value.class.name}" unless value.is_a?(Hash)
            return if value.empty?

            value = LedgerSync::Util::HashHelpers.deep_stringify_keys(value)
            resource_class = Adaptor.resource_from_ledger_type(type: value['type'])
            raise "Unknown QuickBooks Online resource type: #{value['type']}" if resource_class.blank?

            ret = resource_class.new(
              ledger_id: value['value']
            )

            ret.display_name = value['name'] if ret.respond_to?(:display_name)
            ret
          end

          def convert_from_local(value:)
            return if value.nil?
            raise "Resource expected.  Given: #{value.class.name}" unless value.is_a?(LedgerSync::Resource)

            ret = {
              'value' => value.ledger_id
            }
            ret['name'] = value.display_name if value.respond_to?(:display_name)
            ret['type'] = Adaptor.ledger_resource_type_for(
              resource_class: value.class
            ).classify
            ret
          end

          private

          def string_helpers
            @string_helpers ||= LedgerSync::Util::StringHelpers
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ledger_sync-1.3.5 lib/ledger_sync/adaptors/quickbooks_online/ledger_serializer_type/entity_reference_type.rb
ledger_sync-1.3.4 lib/ledger_sync/adaptors/quickbooks_online/ledger_serializer_type/entity_reference_type.rb
ledger_sync-1.3.3 lib/ledger_sync/adaptors/quickbooks_online/ledger_serializer_type/entity_reference_type.rb
ledger_sync-1.3.2 lib/ledger_sync/adaptors/quickbooks_online/ledger_serializer_type/entity_reference_type.rb
ledger_sync-1.3.1 lib/ledger_sync/adaptors/quickbooks_online/ledger_serializer_type/entity_reference_type.rb