Sha256: 164cf955cc50931711051a852c3335078be2ad9b9053c4622f4c630809905502

Contents?: true

Size: 1.87 KB

Versions: 28

Compression:

Stored size: 1.87 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

      def self.statuses
        Workarea.config.inventory_status_calculators.map do |klass|
          klass.demodulize.underscore
        end
      end

      statuses.each do |status_name|
        define_method "#{status_name}?" do
          status.to_s == status_name
        end
      end

      # The total number available to sell
      #
      # return [Integer]
      #
      def available_to_sell
        records.map(&:available_to_sell).sum
      end

      # If the product has any puchasable inventory
      #
      # return [Boolean]
      #
      def purchasable?(quantity = 1)
        quantity <= available_to_sell
      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

      # Get the status of this collection of inventory skus.
      #
      # @return [Symbol]
      #
      def status
        calculators = Workarea.config.inventory_status_calculators.map(&:constantize)
        StatusCalculator.new(calculators, self).result
      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

28 entries across 28 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/inventory/collection.rb
workarea-core-3.5.26 app/models/workarea/inventory/collection.rb
workarea-core-3.5.25 app/models/workarea/inventory/collection.rb
workarea-core-3.5.23 app/models/workarea/inventory/collection.rb
workarea-core-3.5.22 app/models/workarea/inventory/collection.rb
workarea-core-3.5.21 app/models/workarea/inventory/collection.rb
workarea-core-3.5.20 app/models/workarea/inventory/collection.rb
workarea-core-3.5.19 app/models/workarea/inventory/collection.rb
workarea-core-3.5.18 app/models/workarea/inventory/collection.rb
workarea-core-3.5.17 app/models/workarea/inventory/collection.rb
workarea-core-3.5.16 app/models/workarea/inventory/collection.rb
workarea-core-3.5.15 app/models/workarea/inventory/collection.rb
workarea-core-3.5.14 app/models/workarea/inventory/collection.rb
workarea-core-3.5.13 app/models/workarea/inventory/collection.rb
workarea-core-3.5.12 app/models/workarea/inventory/collection.rb
workarea-core-3.5.11 app/models/workarea/inventory/collection.rb
workarea-core-3.5.10 app/models/workarea/inventory/collection.rb
workarea-core-3.5.9 app/models/workarea/inventory/collection.rb
workarea-core-3.5.8 app/models/workarea/inventory/collection.rb
workarea-core-3.5.7 app/models/workarea/inventory/collection.rb