Class: InventoryRefresh::InventoryCollection::ReferencesStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/inventory_refresh/inventory_collection/references_storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index_proxy) ⇒ ReferencesStorage

Returns a new instance of ReferencesStorage.



12
13
14
15
16
17
# File 'lib/inventory_refresh/inventory_collection/references_storage.rb', line 12

def initialize(index_proxy)
  @index_proxy                   = index_proxy
  @references                    = {}
  @references[primary_index_ref] = {}
  @attribute_references          = Set.new
end

Instance Attribute Details

#attribute_referencesSet (readonly)

Returns A set of InventoryObject attributes names, which tells us InventoryObject attributes were referenced by other InventoryObject objects using a lazy_find with :key.

Returns:

  • (Set)

    A set of InventoryObject attributes names, which tells us InventoryObject attributes were referenced by other InventoryObject objects using a lazy_find with :key.



10
11
12
# File 'lib/inventory_refresh/inventory_collection/references_storage.rb', line 10

def attribute_references
  @attribute_references
end

#referencesHash (readonly)

Returns A set of InventoryObjects manager_uuids, which tells us which InventoryObjects were referenced by other InventoryObjects using a lazy_find.

Returns:

  • (Hash)

    A set of InventoryObjects manager_uuids, which tells us which InventoryObjects were referenced by other InventoryObjects using a lazy_find.



6
7
8
# File 'lib/inventory_refresh/inventory_collection/references_storage.rb', line 6

def references
  @references
end

Instance Method Details

#<<(reference_data) ⇒ Object

Adds reference to the storage. The reference can be already existing, otherwise we attempt to build it. This is simplified version of add_reference, not allowing to define :key or :ref.

Parameters:

  • reference_data (InventoryRefresh::InventoryCollection::References, Hash, Object)

    Either existing Reference object, or data we will build the reference object from. For InventoryCollection with :manager_ref size bigger than 1, it’s required to pass a Hash.



44
45
46
# File 'lib/inventory_refresh/inventory_collection/references_storage.rb', line 44

def <<(reference_data)
  add_reference(reference_data)
end

#add_reference(reference_data, key: nil, ref: nil) ⇒ Object

Adds reference to the storage. The reference can be already existing, otherwise we attempt to build it.

Parameters:

  • reference_data (InventoryRefresh::InventoryCollection::References, Hash, Object)

    Either existing Reference object, or data we will build the reference object from. For InventoryCollection with :manager_ref size bigger than 1, it’s required to pass a Hash.

  • key (String) (defaults to: nil)

    If the reference comes from a InventoryObjectLazy, pointing to specific attribute using :key we want to record what attribute was referenced.

  • ref (Symbol) (defaults to: nil)

    A key to specific reference, if it’s a reference pointing to something else than primary index.



28
29
30
31
32
33
34
35
36
# File 'lib/inventory_refresh/inventory_collection/references_storage.rb', line 28

def add_reference(reference_data, key: nil, ref: nil)
  reference           = build_reference(reference_data, ref)
  specific_references = references[reference.ref] ||= {}

  specific_references[reference.stringified_reference] = reference

  # If we access an attribute of the value, using a :key, we want to keep a track of that
  attribute_references << key if key
end

#build_reference(reference_data, ref = nil) ⇒ Object

Builds a Reference object

Parameters:

  • reference_data (InventoryRefresh::InventoryCollection::References, Hash, Object)

    Either existing Reference object, or data we will build the reference object from. For InventoryCollection with :manager_ref size bigger than 1, it’s required to pass a Hash.



70
71
72
73
74
75
# File 'lib/inventory_refresh/inventory_collection/references_storage.rb', line 70

def build_reference(reference_data, ref = nil)
  ref ||= primary_index_ref
  return reference_data if reference_data.kind_of?(::InventoryRefresh::InventoryCollection::Reference)

  ::InventoryRefresh::InventoryCollection::Reference.new(reference_data, ref, named_ref(ref))
end

#build_stringified_reference(hash, keys) ⇒ String

Builds string uuid from passed Hash and keys

Parameters:

  • hash (Hash)

    Hash data

  • keys (Array<Symbol>)

    Indexes into the Hash data

Returns:

  • (String)

    Concatenated values on keys from data



82
83
84
# File 'lib/inventory_refresh/inventory_collection/references_storage.rb', line 82

def build_stringified_reference(hash, keys)
  ::InventoryRefresh::InventoryCollection::Reference.build_stringified_reference(hash, keys)
end

#build_stringified_reference_for_record(record, keys) ⇒ String

Builds string uuid from passed Object and keys

Parameters:

  • record (ApplicationRecord)

    ActiveRecord record

  • keys (Array<Symbol>)

    Indexes into the Hash data

Returns:

  • (String)

    Concatenated values on keys from data



91
92
93
# File 'lib/inventory_refresh/inventory_collection/references_storage.rb', line 91

def build_stringified_reference_for_record(record, keys)
  ::InventoryRefresh::InventoryCollection::Reference.build_stringified_reference_for_record(record, keys)
end

#merge!(references_array, ref: nil) ⇒ InventoryRefresh::InventoryCollection::ReferencesStorage

Adds array of references to the storage. The reference can be already existing, otherwise we attempt to build it.

Parameters:

  • references_array (Array)

    Array of reference objects acceptable by add_reference method.

  • ref (Symbol) (defaults to: nil)

    A key to specific reference, if it’s a reference pointing to something else than primary index.

Returns:



55
56
57
58
# File 'lib/inventory_refresh/inventory_collection/references_storage.rb', line 55

def merge!(references_array, ref: nil)
  references_array.each { |reference_data| add_reference(reference_data, :ref => ref) }
  self
end

#primary_referencesHash{String => InventoryRefresh::InventoryCollection::Reference}

Returns Hash of indexed Reference objects.

Returns:



61
62
63
# File 'lib/inventory_refresh/inventory_collection/references_storage.rb', line 61

def primary_references
  references[primary_index_ref]
end