Class: LedgerSync::ResourceAttributeSet

Inherits:
Object
  • Object
show all
Includes:
Util::Mixins::DelegateIterableMethodsMixin
Defined in:
lib/ledger_sync/resource_attribute_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Mixins::DelegateIterableMethodsMixin

included

Constructor Details

#initialize(resource:) ⇒ ResourceAttributeSet

Returns a new instance of ResourceAttributeSet.



17
18
19
20
21
22
23
24
# File 'lib/ledger_sync/resource_attribute_set.rb', line 17

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

  @resource = resource
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/ledger_sync/resource_attribute_set.rb', line 7

def attributes
  @attributes
end

#referencesObject (readonly)

Returns the value of attribute references.



7
8
9
# File 'lib/ledger_sync/resource_attribute_set.rb', line 7

def references
  @references
end

#references_manyObject (readonly)

Returns the value of attribute references_many.



7
8
9
# File 'lib/ledger_sync/resource_attribute_set.rb', line 7

def references_many
  @references_many
end

#references_oneObject (readonly)

Returns the value of attribute references_one.



7
8
9
# File 'lib/ledger_sync/resource_attribute_set.rb', line 7

def references_one
  @references_one
end

#resourceObject (readonly)

Returns the value of attribute resource.



7
8
9
# File 'lib/ledger_sync/resource_attribute_set.rb', line 7

def resource
  @resource
end

Instance Method Details

#add(attribute) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ledger_sync/resource_attribute_set.rb', line 26

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

#to_aObject



46
47
48
# File 'lib/ledger_sync/resource_attribute_set.rb', line 46

def to_a
  attributes.values
end

#to_hObject



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

def to_h
  attributes.transform_values(&:value)
end