Sha256: ac126697de5f0c87e8d085c5a43c7131b3225bc8da3a8d191c487eae94eab809

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module LedgerSync
  module Adaptors
    class LedgerSerializerAttributeSet
      attr_reader :attributes,
                  :id_attribute,
                  :ledger_attribute_keyed_hash,
                  :resource_attribute_keyed_hash,
                  :serializer_class

      delegate  :[],
                :each,
                :include?,
                :map,
                to: :attributes

      def initialize(serializer_class:)
        @attributes = []
        @ledger_attribute_keyed_hash = {}
        @resource_attribute_keyed_hash = {}
        @serializer_class = serializer_class
      end

      def add(attribute)
        @attributes << attribute
        @id_attribute = attribute if attribute.id
        if attribute.ledger_attribute.present?
          raise "ledger_attribute already defined for #{serializer_class.name}: #{attribute.ledger_attribute}" if ledger_attribute_keyed_hash.key?(attribute.ledger_attribute.to_s)

          ledger_attribute_keyed_hash[attribute.ledger_attribute.to_s] = attribute
        end

        if attribute.resource_attribute.present?
          raise "resource_attribute already defined for #{serializer_class.name}: #{attribute.resource_attribute}" if resource_attribute_keyed_hash.key?(attribute.resource_attribute.to_s)

          resource_attribute_keyed_hash[attribute.resource_attribute.to_s] = attribute
        end

        # TODO: Can make this validate something like `expense.vendor.id`
        # raise "#{resource_attribute} is not an attribute of the resource #{_inferred_resource_class}" if !resource_attribute.nil? && !_inferred_resource_class.serialize_attribute?(resource_attribute)

        attribute
      end

      def ledger_attribute?(key)
        ledger_attribute_keyed_hash.include?(key.to_s)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ledger_sync-1.1.3 lib/ledger_sync/adaptors/ledger_serializer_attribute_set.rb
ledger_sync-1.1.2 lib/ledger_sync/adaptors/ledger_serializer_attribute_set.rb