class InventoryRefresh::InventoryCollection::Index::Type::Base

Attributes

attribute_names[R]
index[RW]
index_name[R]
inventory_collection[R]

Public Class Methods

new(inventory_collection, index_name, attribute_names, *_args) click to toggle source

@param inventory_collection [InventoryRefresh::InventoryCollection] InventoryCollection owning the index @param index_name

# File lib/inventory_refresh/inventory_collection/index/type/base.rb, line 10
def initialize(inventory_collection, index_name, attribute_names, *_args)
  @index = {}

  @inventory_collection = inventory_collection
  @index_name           = index_name
  @attribute_names      = attribute_names

  assert_attribute_names!
end

Public Instance Methods

find(_index_value) click to toggle source

Find value based on index_value

@param _index_value [String] a index_value of the InventoryObject we search for

# File lib/inventory_refresh/inventory_collection/index/type/base.rb, line 38
def find(_index_value)
  raise "Implement in subclass"
end
index_data() click to toggle source

@return [Array] Returns index data

# File lib/inventory_refresh/inventory_collection/index/type/base.rb, line 31
def index_data
  index.values
end
store_index_for(inventory_object) click to toggle source

Indexes passed InventoryObject

@param inventory_object [InventoryRefresh::InventoryObject] InventoryObject we want to index @return [InventoryRefresh::InventoryObject] InventoryObject object

# File lib/inventory_refresh/inventory_collection/index/type/base.rb, line 26
def store_index_for(inventory_object)
  index[build_stringified_reference(inventory_object.data, attribute_names)] = inventory_object
end

Private Instance Methods

assert_attribute_names!() click to toggle source

Asserts that InventoryCollection model has attributes specified for index

# File lib/inventory_refresh/inventory_collection/index/type/base.rb, line 53
def assert_attribute_names!
  # Skip for manually defined nodes
  return if model_class.nil?
  # When we do custom saving, we allow any indexes to be passed, to no limit the user
  return unless custom_save_block.nil?

  # We cannot simply do model_class.method_defined?(attribute_name.to_sym), because e.g. db attributes seems
  # to be create lazily
  test_model_object = model_class.new
  attribute_names.each do |attribute_name|
    unless test_model_object.respond_to?(attribute_name.to_sym)
      raise "Invalid definition of index :#{index_name}, there is no attribute :#{attribute_name} on model #{model_class}"
    end
  end
end