Class: InventoryRefresh::InventoryCollection::DataStorage
- Inherits:
-
Object
- Object
- InventoryRefresh::InventoryCollection::DataStorage
- Defined in:
- lib/inventory_refresh/inventory_collection/data_storage.rb
Instance Attribute Summary collapse
-
#data ⇒ Array<InventoryObject>
Objects of the InventoryCollection in an Array.
-
#index_proxy ⇒ Object
readonly
Returns the value of attribute index_proxy.
-
#inventory_collection ⇒ Object
readonly
Returns the value of attribute inventory_collection.
Instance Method Summary collapse
-
#<<(inventory_object) ⇒ InventoryRefresh::InventoryCollection
(also: #push)
Adds passed InventoryObject into the InventoryCollection’s storage.
-
#build(hash) ⇒ InventoryRefresh::InventoryObject
Finds of builds a new InventoryObject.
-
#build_partial(hash) ⇒ InventoryRefresh::InventoryObject
Finds of builds a new InventoryObject with incomplete data.
-
#find_or_build(manager_uuid) ⇒ InventoryRefresh::InventoryObject
Finds of builds a new InventoryObject.
-
#find_or_build_by(hash) ⇒ InventoryRefresh::InventoryObject
Finds of builds a new InventoryObject.
- #from_hash(inventory_objects_data, available_inventory_collections) ⇒ Object
-
#initialize(inventory_collection, secondary_refs) ⇒ DataStorage
constructor
A new instance of DataStorage.
-
#to_a ⇒ Array<InventoryRefresh::InventoryObject>
Returns array of built InventoryObject objects.
- #to_hash ⇒ Object
Constructor Details
#initialize(inventory_collection, secondary_refs) ⇒ DataStorage
Returns a new instance of DataStorage.
31 32 33 34 35 36 |
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 31 def initialize(inventory_collection, secondary_refs) @inventory_collection = inventory_collection @data = [] @index_proxy = InventoryRefresh::InventoryCollection::Index::Proxy.new(inventory_collection, secondary_refs) end |
Instance Attribute Details
#data ⇒ Array<InventoryObject>
Returns objects of the InventoryCollection in an Array.
7 8 9 |
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 7 def data @data end |
#index_proxy ⇒ Object (readonly)
Returns the value of attribute index_proxy.
9 10 11 |
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 9 def index_proxy @index_proxy end |
#inventory_collection ⇒ Object (readonly)
Returns the value of attribute inventory_collection.
9 10 11 |
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 9 def inventory_collection @inventory_collection end |
Instance Method Details
#<<(inventory_object) ⇒ InventoryRefresh::InventoryCollection Also known as: push
Adds passed InventoryObject into the InventoryCollection’s storage
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 42 def <<(inventory_object) if inventory_object.manager_uuid.present? && !primary_index.find(inventory_object.manager_uuid) data << inventory_object # TODO(lsmola) Maybe we do not need the secondary indexes here? # Maybe we should index it like LocalDb indexes, on demand, and storing what was # indexed? Maybe we should allow only lazy access and no direct find from a parser. Since for streaming # refresh, things won't be parsed together and no full state will be taken. build_primary_index_for(inventory_object) build_secondary_indexes_for(inventory_object) end inventory_collection end |
#build(hash) ⇒ InventoryRefresh::InventoryObject
Finds of builds a new InventoryObject. By building it, we also put in into the InventoryCollection’s storage.
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/data_storage.rb', line 78 def build(hash) hash, uuid, inventory_object = primary_index_scan(hash) # Return InventoryObject if found in primary index return inventory_object unless inventory_object.nil? # We will take existing skeletal record, so we don't duplicate references for saving. We can have duplicated # reference from local_db index, (if we are using .find in parser, that causes N+1 db queries), but that is ok, # since that one is not being saved. inventory_object = skeletal_primary_index.delete(uuid) # We want to update the skeletal record with actual data inventory_object&.assign_attributes(hash) # Build the InventoryObject inventory_object ||= new_inventory_object(enrich_data(hash)) # Store new InventoryObject and return it push(inventory_object) inventory_object end |
#build_partial(hash) ⇒ InventoryRefresh::InventoryObject
Finds of builds a new InventoryObject with incomplete data.
105 106 107 |
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 105 def build_partial(hash) skeletal_primary_index.build(hash) end |
#find_or_build(manager_uuid) ⇒ InventoryRefresh::InventoryObject
Finds of builds a new InventoryObject. By building it, we also put in into the InventoryCollection’s storage.
62 63 64 65 66 |
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 62 def find_or_build(manager_uuid) raise "The uuid consists of #{manager_ref.size} attributes, please find_or_build_by method" if manager_ref.size > 1 find_or_build_by(manager_ref.first => manager_uuid) end |
#find_or_build_by(hash) ⇒ InventoryRefresh::InventoryObject
Finds of builds a new InventoryObject. By building it, we also put in into the InventoryCollection’s storage.
69 70 71 |
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 69 def find_or_build_by(hash) build(hash) end |
#from_hash(inventory_objects_data, available_inventory_collections) ⇒ Object
120 121 122 123 124 |
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 120 def from_hash(inventory_objects_data, available_inventory_collections) InventoryRefresh::InventoryCollection::Serialization .new(inventory_collection) .from_hash(inventory_objects_data, available_inventory_collections) end |
#to_a ⇒ Array<InventoryRefresh::InventoryObject>
Returns array of built InventoryObject objects
112 113 114 |
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 112 def to_a data end |
#to_hash ⇒ Object
116 117 118 |
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 116 def to_hash InventoryRefresh::InventoryCollection::Serialization.new(inventory_collection).to_hash end |