Sha256: ce475a93a71f5b336d9b15a281905001ea010e38af1515cbebded3a76dce548c

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 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)

      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
      attributes.transform_values(&:value)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ledger_sync-1.4.4 lib/ledger_sync/resource_attribute_set.rb