Class: Workarea::Inventory::Collection
- Inherits:
-
Object
- Object
- Workarea::Inventory::Collection
- Includes:
- Enumerable
- Defined in:
- app/models/workarea/inventory/collection.rb
Overview
Instance Attribute Summary collapse
-
#skus ⇒ Object
readonly
Returns the value of attribute skus.
Class Method Summary collapse
Instance Method Summary collapse
-
#available_to_sell ⇒ Object
The total number available to sell.
-
#for_sku(sku) ⇒ Sku
Grab the specific Sku for a SKU.
-
#initialize(skus, records = nil) ⇒ Collection
constructor
A new instance of Collection.
- #policies ⇒ Object
-
#purchasable?(quantity = 1) ⇒ Boolean
If the product has any puchasable inventory.
- #records ⇒ Object
-
#status ⇒ Symbol
Get the status of this collection of inventory skus.
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
#skus ⇒ Object (readonly)
Returns the value of attribute skus.
9 10 11 |
# File 'app/models/workarea/inventory/collection.rb', line 9 def skus @skus end |
Class Method Details
.statuses ⇒ Object
17 18 19 20 21 |
# File 'app/models/workarea/inventory/collection.rb', line 17 def self.statuses Workarea.config.inventory_status_calculators.map do |klass| klass.demodulize.underscore end end |
Instance Method Details
#available_to_sell ⇒ Object
The total number available to sell
return [Integer]
33 34 35 |
# File 'app/models/workarea/inventory/collection.rb', line 33 def available_to_sell records.map(&:available_to_sell).sum end |
#for_sku(sku) ⇒ Sku
51 52 53 |
# File 'app/models/workarea/inventory/collection.rb', line 51 def for_sku(sku) records.detect { |r| r.id == sku } end |
#policies ⇒ Object
55 56 57 |
# File 'app/models/workarea/inventory/collection.rb', line 55 def policies records.map(&:policy) end |
#purchasable?(quantity = 1) ⇒ Boolean
If the product has any puchasable inventory
return [Boolean]
41 42 43 |
# File 'app/models/workarea/inventory/collection.rb', line 41 def purchasable?(quantity = 1) quantity <= available_to_sell end |
#records ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'app/models/workarea/inventory/collection.rb', line 69 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 |
#status ⇒ Symbol
Get the status of this collection of inventory skus.
63 64 65 66 |
# File 'app/models/workarea/inventory/collection.rb', line 63 def status calculators = Workarea.config.inventory_status_calculators.map(&:constantize) StatusCalculator.new(calculators, self).result end |