Sha256: 8a1b43ff2133ef1711a98a22e00644fd866ffd123dc42e2b616f6b381b20f79b
Contents?: true
Size: 1.15 KB
Versions: 34
Compression:
Stored size: 1.15 KB
Contents
module Workarea module Inventory # This class represents a collection of {Sku}s. It is more # performant because it does a single query to grab the # {Sku}s instead of N+1. # class Collection include Enumerable attr_reader :skus delegate :any?, :empty?, :each, :size, :length, to: :records def initialize(skus, records = nil) @skus = skus @records = records end # The total number available to sell # # return [Integer] # def available_to_sell records.map(&:available_to_sell).sum end # Grab the specific {Sku} for a SKU. # Returns a new, unpersisted {Sku} if one does not exist. # # @param sku [String] # @return [Sku] # def for_sku(sku) records.detect { |r| r.id == sku } end def policies records.map(&:policy) end def records @records ||= begin existing = Sku.in(id: skus).to_a missing_skus = skus - existing.map(&:id) existing + missing_skus.map { |sku| Sku.new(id: sku ) } end end end end end
Version data entries
34 entries across 34 versions & 1 rubygems