Sha256: 2b26f56da9280ef33ec7a3e1c07c524ae1a971f0339dcf60178cbd039d734ed5

Contents?: true

Size: 1.34 KB

Versions: 6

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module LedgerSync
  class ResourceAttributeSet
    attr_reader :attributes,
                :references,
                :references_one,
                :references_many,
                :resource

    delegate  :[],
              :each,
              :include?,
              :key?,
              :keys,
              :map,
              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)

      if attribute.is_a?(ResourceAttribute::Reference::One)
        @attributes[attribute.name] = attribute
        @references << attribute
        @references_one << attribute
      elsif attribute.is_a?(ResourceAttribute::Reference::Many)
        @attributes[attribute.name] = attribute
        @references << attribute
        @references_many << attribute
      elsif attribute.is_a?(ResourceAttribute)
        @attributes[attribute.name] = attribute
      else
        raise 'Unknown attribute class'
      end
    end

    def to_a
      attributes.values
    end

    def to_h
      Hash[attributes.map { |k, v| [k, v.value] }]
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ledger_sync-1.3.4 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-1.3.3 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-1.3.2 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-1.3.1 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-1.1.3 lib/ledger_sync/resource_attribute_set.rb
ledger_sync-1.1.2 lib/ledger_sync/resource_attribute_set.rb