Sha256: cd97946a19efaa468a2aaa409d5dde771f9f077dc3db52f5aa562a95c8a9dd1c

Contents?: true

Size: 1.25 KB

Versions: 19

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module LedgerSync
  class ResourceAttributeSet
    include Util::Mixins::DelegateIterableMethodsMixin

    attr_reader :attributes,
                :references,
                :references_one,
                :references_many,
                :resource

    delegate_hash_methods_to :attributes

    alias names keys

    def initialize(resource:)
      @attributes = {}
      @references = []
      @references_one = []
      @references_many = []

      @resource = resource
    end

    def add(attribute)
      name = attribute.name
      raise "Attribute #{name} already exists on #{resource.name}." if attributes.key?(name)

      case attribute
      when ResourceAttribute::Reference::One
        @attributes[attribute.name] = attribute
        @references << attribute
        @references_one << attribute
      when ResourceAttribute::Reference::Many
        @attributes[attribute.name] = attribute
        @references << attribute
        @references_many << attribute
      when ResourceAttribute
        @attributes[attribute.name] = attribute
      else
        raise 'Unknown attribute class'
      end
    end

    def to_a
      attributes.values
    end

    def to_h
      attributes.transform_values(&:value)
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ledger_sync-3.0.0 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-2.6.0 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-2.5.0 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-2.4.4 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-2.3.1 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-2.2.3 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-2.2.1 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-2.2.0 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-2.0.2 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-2.0.1 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-2.0.0 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-2.0.0.pre.1 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-1.8.1 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-1.8.0 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-1.7.0 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-1.6.0 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-1.5.2 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-1.5.1 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-1.5.0 lib/ledger_sync/resource_attribute_set.rb