Sha256: 5c53d14bf9e1503f8e318c9050243cae7c43e0bf420cbcfd5d129c6c8b5260f9

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

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

      case attribute
      when ResourceAttribute
        nil
      when ResourceAttribute::Reference::One
        @references << attribute
        @references_one << attribute
      when ResourceAttribute::Reference::Many
        @references << attribute
        @references_many << attribute
      else
        raise 'Unknown attribute class'
      end

      @attributes[attribute.name] = attribute
    end

    def to_a
      attributes.values
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

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