class InventoryRefresh::InventoryCollection::Scanner

Attributes

associations_hash[R]
indexed_inventory_collections[R]
inventory_collection[R]

Public Class Methods

build_association_hash(inventory_collections) click to toggle source
# File lib/inventory_refresh/inventory_collection/scanner.rb, line 26
def build_association_hash(inventory_collections)
  associations_hash = {}
  parents = inventory_collections.map(&:parent).compact.uniq
  parents.each do |parent|
    parent.class.reflect_on_all_associations(:has_many).each do |association|
      through_assoc = association.options.try(:[], :through)
      associations_hash[association.name] = through_assoc if association.options.try(:[], :through)
    end
  end
  associations_hash
end
new(inventory_collection, indexed_inventory_collections, associations_hash) click to toggle source
# File lib/inventory_refresh/inventory_collection/scanner.rb, line 65
def initialize(inventory_collection, indexed_inventory_collections, associations_hash)
  @inventory_collection          = inventory_collection
  @indexed_inventory_collections = indexed_inventory_collections
  @associations_hash             = associations_hash
end
scan!(inventory_collections) click to toggle source

Scanning inventory_collections for dependencies and references, storing the results in the inventory_collections themselves. Dependencies are needed for building a graph, references are needed for effective DB querying, where we can load all referenced objects of some InventoryCollection by one DB query.

@param inventory_collections [Array<InventoryRefresh::InventoryCollection>] Array of InventoryCollection objects

# File lib/inventory_refresh/inventory_collection/scanner.rb, line 12
def scan!(inventory_collections)
  indexed_inventory_collections = inventory_collections.index_by(&:name)

  inventory_collections.each do |inventory_collection|
    new(inventory_collection, indexed_inventory_collections, build_association_hash(inventory_collections)).scan!
  end

  inventory_collections.each do |inventory_collection|
    inventory_collection.dependencies.each do |dependency|
      dependency.dependees << inventory_collection
    end
  end
end

Public Instance Methods

scan!() click to toggle source
# File lib/inventory_refresh/inventory_collection/scanner.rb, line 71
def scan!
  # Scan InventoryCollection InventoryObjects and store the results inside of the InventoryCollection
  data.each do |inventory_object|
    scan_inventory_object!(inventory_object)
  end

  # Scan InventoryCollection skeletal data
  inventory_collection.skeletal_primary_index.each_value do |inventory_object|
    scan_inventory_object!(inventory_object)
  end

  # Mark InventoryCollection as finalized aka. scanned
  self.data_collection_finalized = true
end

Private Instance Methods

add_reference(value_inventory_collection, value) click to toggle source
# File lib/inventory_refresh/inventory_collection/scanner.rb, line 102
def add_reference(value_inventory_collection, value)
  value_inventory_collection.add_reference(value.reference, :key => value.key)
end
loadable?(value) click to toggle source
# File lib/inventory_refresh/inventory_collection/scanner.rb, line 98
def loadable?(value)
  inventory_object_lazy?(value) || inventory_object?(value)
end
scan_inventory_object!(inventory_object) click to toggle source
# File lib/inventory_refresh/inventory_collection/scanner.rb, line 88
def scan_inventory_object!(inventory_object)
  inventory_object.data.each do |key, value|
    if value.kind_of?(Array)
      value.each { |val| scan_inventory_object_attribute!(key, val) }
    else
      scan_inventory_object_attribute!(key, value)
    end
  end
end
scan_inventory_object_attribute!(key, value) click to toggle source
# File lib/inventory_refresh/inventory_collection/scanner.rb, line 106
def scan_inventory_object_attribute!(key, value)
  return unless loadable?(value)
  value_inventory_collection = value.inventory_collection

  # Storing attributes and their dependencies
  (dependency_attributes[key] ||= Set.new) << value_inventory_collection if value.dependency?

  # Storing a reference in the target inventory_collection, then each IC knows about all the references and can
  # e.g. load all the referenced uuids from a DB
  add_reference(value_inventory_collection, value)

  if inventory_object_lazy?(value)
    # Storing if attribute is a transitive dependency, so a lazy_find :key results in dependency
    transitive_dependency_attributes << key if value.transitive_dependency?
  end
end