Class: InventoryRefresh::InventoryCollection::Serialization

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inventory_collection) ⇒ Serialization

Returns a new instance of Serialization.

Parameters:



21
22
23
# File 'lib/inventory_refresh/inventory_collection/serialization.rb', line 21

def initialize(inventory_collection)
  @inventory_collection = inventory_collection
end

Instance Attribute Details

#inventory_collectionObject (readonly)

Returns the value of attribute inventory_collection.



17
18
19
# File 'lib/inventory_refresh/inventory_collection/serialization.rb', line 17

def inventory_collection
  @inventory_collection
end

Instance Method Details

#from_hash(inventory_objects_data, available_inventory_collections) ⇒ Object

Loads InventoryCollection data from it’s serialized form into existing InventoryCollection object

Parameters:

  • inventory_objects_data (Hash)

    Serialized InventoryCollection as Hash

  • available_inventory_collections (Array<InventoryRefresh::InventoryCollection>)

    List of available InventoryCollection objects



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/inventory_refresh/inventory_collection/serialization.rb', line 30

def from_hash(inventory_objects_data, available_inventory_collections)
  targeted_scope.merge!(inventory_objects_data["manager_uuids"].map(&:symbolize_keys!)) if inventory_objects_data["manager_uuids"]

  (inventory_objects_data['data'] || []).each do |inventory_object_data|
    build(hash_to_data(inventory_object_data, available_inventory_collections).symbolize_keys!)
  end

  (inventory_objects_data['partial_data'] || []).each do |inventory_object_data|
    skeletal_primary_index.build(hash_to_data(inventory_object_data, available_inventory_collections).symbolize_keys!)
  end

  self.all_manager_uuids = inventory_objects_data['all_manager_uuids']
end

#to_hashHash

Serializes InventoryCollection’s data storage into Array of Hashes

Returns:

  • (Hash)

    Serialized InventoryCollection object into Hash



47
48
49
50
51
52
53
54
55
56
# File 'lib/inventory_refresh/inventory_collection/serialization.rb', line 47

def to_hash
  {
    :name              => name,
    # TODO(lsmola) we do not support nested references here, should we?
    :manager_uuids     => targeted_scope.primary_references.values.map(&:full_reference),
    :all_manager_uuids => all_manager_uuids,
    :data              => data.map { |x| data_to_hash(x.data) },
    :partial_data      => skeletal_primary_index.index_data.map { |x| data_to_hash(x.data) },
  }
end