Class: InventoryRefresh::InventoryCollection::Index::Type::Skeletal

Inherits:
Base
  • Object
show all
Defined in:
lib/inventory_refresh/inventory_collection/index/type/skeletal.rb

Instance Method Summary collapse

Methods inherited from Base

#index_data, #store_index_for

Constructor Details

#initialize(inventory_collection, index_name, attribute_names, primary_index) ⇒ Skeletal

Returns a new instance of Skeletal.

Parameters:



11
12
13
14
15
# File 'lib/inventory_refresh/inventory_collection/index/type/skeletal.rb', line 11

def initialize(inventory_collection, index_name, attribute_names, primary_index)
  super

  @primary_index = primary_index
end

Instance Method Details

#build(attributes) ⇒ InventoryObject

Builds index record with skeletal InventoryObject and returns it. Or it returns existing InventoryObject that is found in primary_index or skeletal_primary_index.

Parameters:

  • attributes (Hash)

    Skeletal data of the index, must contain unique index keys and everything else needed for creating the record in the Database

Returns:

  • (InventoryObject)

    Returns built InventoryObject or existing InventoryObject with new attributes assigned



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/inventory_refresh/inventory_collection/index/type/skeletal.rb', line 62

def build(attributes)
  attributes = {}.merge!(default_values).merge!(attributes)
  fill_versions!(attributes)

  # If the primary index is already filled, we don't want populate skeletal index
  uuid = ::InventoryRefresh::InventoryCollection::Reference.build_stringified_reference(attributes, named_ref)
  if (inventory_object = primary_index.find(uuid))
    return inventory_object.assign_attributes(attributes)
  end

  # Return if skeletal index already exists
  if (inventory_object = index[uuid])
    return inventory_object.assign_attributes(attributes)
  end

  # We want to populate a new skeletal index
  inventory_object                     = new_inventory_object(attributes)
  index[inventory_object.manager_uuid] = inventory_object
end

#delete(index_value) ⇒ InventoryObject|nil

Deletes and returns the value on the index_value

Parameters:

  • index_value (String)

    a index_value of the InventoryObject we search for

Returns:



39
40
41
# File 'lib/inventory_refresh/inventory_collection/index/type/skeletal.rb', line 39

def delete(index_value)
  index.delete(index_value)
end

#find(index_value) ⇒ InventoryObject|nil

Find value based on index_value

Parameters:

  • index_value (String)

    a index_value of the InventoryObject we search for

Returns:



31
32
33
# File 'lib/inventory_refresh/inventory_collection/index/type/skeletal.rb', line 31

def find(index_value)
  index[index_value]
end

#skeletonize_primary_index(index_value) ⇒ InventoryObject|nil

Takes value from primary_index and inserts it to skeletal index

Parameters:

  • index_value (String)

    a index_value of the InventoryObject we search for

Returns:



47
48
49
50
51
52
53
# File 'lib/inventory_refresh/inventory_collection/index/type/skeletal.rb', line 47

def skeletonize_primary_index(index_value)
  inventory_object = primary_index.delete(index_value)
  return unless inventory_object
  fill_versions!(inventory_object.data)

  index[index_value] = inventory_object
end