Class: LedgerSync::ResourceAttributeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/ledger_sync/resource_attribute_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:) ⇒ ResourceAttributeSet

Returns a new instance of ResourceAttributeSet.



21
22
23
24
25
26
27
28
# File 'lib/ledger_sync/resource_attribute_set.rb', line 21

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

  @resource = resource
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/ledger_sync/resource_attribute_set.rb', line 5

def attributes
  @attributes
end

#referencesObject (readonly)

Returns the value of attribute references.



5
6
7
# File 'lib/ledger_sync/resource_attribute_set.rb', line 5

def references
  @references
end

#references_manyObject (readonly)

Returns the value of attribute references_many.



5
6
7
# File 'lib/ledger_sync/resource_attribute_set.rb', line 5

def references_many
  @references_many
end

#references_oneObject (readonly)

Returns the value of attribute references_one.



5
6
7
# File 'lib/ledger_sync/resource_attribute_set.rb', line 5

def references_one
  @references_one
end

#resourceObject (readonly)

Returns the value of attribute resource.



5
6
7
# File 'lib/ledger_sync/resource_attribute_set.rb', line 5

def resource
  @resource
end

Instance Method Details

#add(attribute) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ledger_sync/resource_attribute_set.rb', line 30

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

#to_aObject



49
50
51
# File 'lib/ledger_sync/resource_attribute_set.rb', line 49

def to_a
  attributes.values
end

#to_hObject



53
54
55
# File 'lib/ledger_sync/resource_attribute_set.rb', line 53

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