class InventoryRefresh::InventoryCollection::Serialization
Attributes
Public Class Methods
@param inventory_collection
[InventoryRefresh::InventoryCollection] InventoryCollection
object we want the storage
for
# File lib/inventory_refresh/inventory_collection/serialization.rb, line 18 def initialize(inventory_collection) @inventory_collection = inventory_collection end
Public Instance Methods
Loads InventoryCollection
data from it's serialized form into existing InventoryCollection
object
@param inventory_objects_data [Hash] Serialized InventoryCollection
as Hash @param available_inventory_collections [Array<InventoryRefresh::InventoryCollection>] List of available
InventoryCollection objects
# File lib/inventory_refresh/inventory_collection/serialization.rb, line 27 def from_hash(inventory_objects_data, available_inventory_collections) (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 end
# File lib/inventory_refresh/inventory_collection/serialization.rb, line 37 def sweep_scope_from_hash(sweep_scope, available_inventory_collections) sweep_scope.map do |s| hash_to_data(s, available_inventory_collections).symbolize_keys! end end
# File lib/inventory_refresh/inventory_collection/serialization.rb, line 54 def sweep_scope_to_hash(sweep_scope) sweep_scope.map { |x| data_to_hash(x) } end
Serializes InventoryCollection's data storage into Array of Hashes
@return [Hash] Serialized InventoryCollection
object into Hash
# File lib/inventory_refresh/inventory_collection/serialization.rb, line 46 def to_hash { :name => name, :data => data.map { |x| data_to_hash(x.data) }, :partial_data => skeletal_primary_index.index_data.map { |x| data_to_hash(x.data) }, } end
Private Instance Methods
Transforms data of the InventoryObject
or Reference
to InventoryObject
into Hash
@param data [Hash] Data of the InventoryObject
or Reference
to InventoryObject
@param depth [Integer] Depth of nesting for nested lazy link @return [Hash] Serialized InventoryObject
or Reference
data into Hash
# File lib/inventory_refresh/inventory_collection/serialization.rb, line 121 def data_to_hash(data, depth = 0) raise "Nested lazy_relation of #{inventory_collection} is too deep, left processing: #{data}" if depth > 20 data.transform_values do |value| if inventory_object_lazy?(value) || inventory_object?(value) lazy_relation_to_hash(value, depth) else value end end end
Converts Hash to attributes usable for building InventoryObject
@param hash [Hash] Serialized InventoryObject
data @param available_inventory_collections [Array<InventoryRefresh::InventoryCollection>] List of available
InventoryCollection objects
@param depth [Integer] Depth of nesting for nested lazy link @return [Hash] Hash with data usable for building InventoryObject
# File lib/inventory_refresh/inventory_collection/serialization.rb, line 104 def hash_to_data(hash, available_inventory_collections, depth = 0) raise "Nested lazy_relation of #{inventory_collection} is too deep, left processing: #{hash}" if depth > 20 hash.transform_values do |value| if value.kind_of?(Hash) && value['inventory_collection_name'] hash_to_lazy_relation(value, available_inventory_collections, depth) else value end end end
Converts Hash to InventoryRefresh::InventoryObjectLazy
@param value [Hash] Serialized InventoryObject
or a lazy link @param available_inventory_collections [Array<InventoryRefresh::InventoryCollection>] List of available
InventoryCollection objects
@param depth [Integer] Depth of nesting for nested lazy link @return [InventoryRefresh::InventoryObjectLazy] Lazy link created from hash
# File lib/inventory_refresh/inventory_collection/serialization.rb, line 84 def hash_to_lazy_relation(value, available_inventory_collections, depth = 0) inventory_collection = available_inventory_collections[value['inventory_collection_name'].try(:to_sym)] raise "Couldn't build lazy_link #{value} the inventory_collection_name was not found" if inventory_collection.blank? inventory_collection.lazy_find( hash_to_data(value['reference'], available_inventory_collections, depth + 1).symbolize_keys!, :ref => value['ref'].try(:to_sym), :key => value['key'].try(:to_sym), :default => value['default'], :transform_nested_lazy_finds => value['transform_nested_lazy_finds'] ) end
Converts InventoryRefresh::InventoryObject
or InventoryRefresh::InventoryObjectLazy
into Hash
@param value [InventoryRefresh::InventoryObject, InventoryRefresh::InventoryObjectLazy] InventoryObject
or a lazy link @param depth [Integer] Depth of nesting for nested lazy link @return [Hash] Serialized InventoryRefresh::InventoryObjectLazy
# File lib/inventory_refresh/inventory_collection/serialization.rb, line 65 def lazy_relation_to_hash(value, depth = 0) { :type => "InventoryRefresh::InventoryObjectLazy", :inventory_collection_name => value.inventory_collection.name, :reference => data_to_hash(value.reference.full_reference, depth + 1), :ref => value.reference.ref, :key => value.try(:key), :default => value.try(:default), :transform_nested_lazy_finds => value.try(:transform_nested_lazy_finds) } end