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

Version Path
workarea-core-3.4.45 app/models/workarea/inventory/collection.rb
workarea-core-3.4.44 app/models/workarea/inventory/collection.rb
workarea-core-3.4.43 app/models/workarea/inventory/collection.rb
workarea-core-3.4.42 app/models/workarea/inventory/collection.rb
workarea-core-3.4.41 app/models/workarea/inventory/collection.rb
workarea-core-3.4.40 app/models/workarea/inventory/collection.rb
workarea-core-3.4.39 app/models/workarea/inventory/collection.rb
workarea-core-3.4.38 app/models/workarea/inventory/collection.rb
workarea-core-3.4.37 app/models/workarea/inventory/collection.rb
workarea-core-3.4.36 app/models/workarea/inventory/collection.rb
workarea-core-3.4.35 app/models/workarea/inventory/collection.rb
workarea-core-3.4.34 app/models/workarea/inventory/collection.rb
workarea-core-3.4.33 app/models/workarea/inventory/collection.rb
workarea-core-3.4.32 app/models/workarea/inventory/collection.rb
workarea-core-3.4.31 app/models/workarea/inventory/collection.rb
workarea-core-3.4.30 app/models/workarea/inventory/collection.rb
workarea-core-3.4.29 app/models/workarea/inventory/collection.rb
workarea-core-3.4.28 app/models/workarea/inventory/collection.rb
workarea-core-3.4.27 app/models/workarea/inventory/collection.rb
workarea-core-3.4.26 app/models/workarea/inventory/collection.rb