Class: SpreeCmCommissioner::RedisStock::CachedInventoryItemsBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree_cm_commissioner/redis_stock/cached_inventory_items_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inventory_items) ⇒ CachedInventoryItemsBuilder

Returns a new instance of CachedInventoryItemsBuilder.



6
7
8
# File 'app/models/spree_cm_commissioner/redis_stock/cached_inventory_items_builder.rb', line 6

def initialize(inventory_items)
  @inventory_items = inventory_items
end

Instance Attribute Details

#inventory_itemsObject (readonly)

Returns the value of attribute inventory_items.



4
5
6
# File 'app/models/spree_cm_commissioner/redis_stock/cached_inventory_items_builder.rb', line 4

def inventory_items
  @inventory_items
end

Instance Method Details

#callObject

output: [ CachedInventoryItem(…), CachedInventoryItem(…) ]



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/spree_cm_commissioner/redis_stock/cached_inventory_items_builder.rb', line 11

def call
  keys = inventory_items.map { |item| "inventory:#{item.id}" }
  return [] unless keys.any?

  counts = SpreeCmCommissioner.redis_pool.with { |redis| redis.mget(*keys) }
  inventory_items.map.with_index do |inventory_item, i|
    ::SpreeCmCommissioner::CachedInventoryItem.new(
      inventory_key: keys[i],
      active: inventory_item.active?,
      quantity_available: cache_inventory(keys[i], inventory_item, counts[i]),
      inventory_item_id: inventory_item.id,
      variant_id: inventory_item.variant_id
    )
  end
end