Class: InventoryRefresh::InventoryCollection::Scanner

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inventory_collection, indexed_inventory_collections, associations_hash) ⇒ Scanner

Returns a new instance of Scanner.



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

def initialize(inventory_collection, indexed_inventory_collections, associations_hash)
  @inventory_collection          = inventory_collection
  @indexed_inventory_collections = indexed_inventory_collections
  @associations_hash             = associations_hash
end

Instance Attribute Details

#associations_hashObject (readonly)

Returns the value of attribute associations_hash.



39
40
41
# File 'lib/inventory_refresh/inventory_collection/scanner.rb', line 39

def associations_hash
  @associations_hash
end

#indexed_inventory_collectionsObject (readonly)

Returns the value of attribute indexed_inventory_collections.



39
40
41
# File 'lib/inventory_refresh/inventory_collection/scanner.rb', line 39

def indexed_inventory_collections
  @indexed_inventory_collections
end

#inventory_collectionObject (readonly)

Returns the value of attribute inventory_collection.



39
40
41
# File 'lib/inventory_refresh/inventory_collection/scanner.rb', line 39

def inventory_collection
  @inventory_collection
end

Class Method Details

.build_association_hash(inventory_collections) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# 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

.scan!(inventory_collections) ⇒ Object

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.

Parameters:



12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Method Details

#scan!Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/inventory_refresh/inventory_collection/scanner.rb', line 77

def scan!
  # Scan InventoryCollection InventoryObjects and store the results inside of the InventoryCollection
  data.each do |inventory_object|
    scan_inventory_object!(inventory_object)

    if targeted? && parent_inventory_collections.blank?
      # We want to track what manager_uuids we should query from a db, for the targeted refresh
      targeted_scope << inventory_object.reference
    end
  end

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

  build_parent_inventory_collections!
  scan_all_manager_uuids_scope!

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