Class: SpreeCmCommissioner::RedisStock::CachedInventoryItemsBuilder
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::RedisStock::CachedInventoryItemsBuilder
- Defined in:
- app/models/spree_cm_commissioner/redis_stock/cached_inventory_items_builder.rb
Instance Attribute Summary collapse
-
#inventory_items ⇒ Object
readonly
Returns the value of attribute inventory_items.
Instance Method Summary collapse
-
#call ⇒ Object
output: [ CachedInventoryItem(…), CachedInventoryItem(…) ].
-
#initialize(inventory_items) ⇒ CachedInventoryItemsBuilder
constructor
A new instance of CachedInventoryItemsBuilder.
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_items ⇒ Object (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
#call ⇒ Object
output: [ CachedInventoryItem(…), CachedInventoryItem(…) ]
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/models/spree_cm_commissioner/redis_stock/cached_inventory_items_builder.rb', line 11 def call stock_keys = inventory_items.map(&:redis_key) hold_keys = inventory_items.map(&:redis_hold_key) all_keys = stock_keys + hold_keys return [] unless all_keys.any? all_counts = SpreeCmCommissioner.inventory_redis_pool.with { |redis| redis.mget(*all_keys) } stock_counts = all_counts.first(inventory_items.length) hold_counts = all_counts.last(inventory_items.length) inventory_items.map.with_index do |inventory_item, i| ::SpreeCmCommissioner::CachedInventoryItem.new( inventory_key: stock_keys[i], inventory_hold_key: hold_keys[i], active: inventory_item.active?, quantity_available: cache_inventory(stock_keys[i], inventory_item, inventory_item.quantity_available, stock_counts[i]), quantity_on_hold: cache_inventory(hold_keys[i], inventory_item, inventory_item.quantity_on_hold, hold_counts[i]), max_capacity: inventory_item.max_capacity, inventory_item_id: inventory_item.id, variant_id: inventory_item.variant_id ) end end |