Class: Workarea::Inventory::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
app/models/workarea/inventory/collection.rb

Overview

This class represents a collection of Skus. It is more performant because it does a single query to grab the Skus instead of N+1.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(skus, records = nil) ⇒ Collection

Returns a new instance of Collection.



12
13
14
15
# File 'app/models/workarea/inventory/collection.rb', line 12

def initialize(skus, records = nil)
  @skus = skus
  @records = records
end

Instance Attribute Details

#skusObject (readonly)

Returns the value of attribute skus.



9
10
11
# File 'app/models/workarea/inventory/collection.rb', line 9

def skus
  @skus
end

Instance Method Details

#available_to_sellObject

The total number available to sell

return [Integer]



21
22
23
# File 'app/models/workarea/inventory/collection.rb', line 21

def available_to_sell
  records.map(&:available_to_sell).sum
end

#for_sku(sku) ⇒ Sku

Grab the specific Sku for a SKU. Returns a new, unpersisted Sku if one does not exist.

Parameters:

Returns:



31
32
33
# File 'app/models/workarea/inventory/collection.rb', line 31

def for_sku(sku)
  records.detect { |r| r.id == sku }
end

#policiesObject



35
36
37
# File 'app/models/workarea/inventory/collection.rb', line 35

def policies
  records.map(&:policy)
end

#recordsObject



39
40
41
42
43
44
45
46
47
# File 'app/models/workarea/inventory/collection.rb', line 39

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