Class: Workarea::Inventory::Capture
- Inherits:
-
Object
- Object
- Workarea::Inventory::Capture
- Defined in:
- app/models/workarea/inventory/capture.rb
Overview
This class is responsible for decrementing inventory levels. Used in Sku#capture, when a Policies::Base wants to actually decrement the levels for purchasing.
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
-
#initialize(sku, available, backordered) ⇒ Capture
constructor
A new instance of Capture.
-
#perform ⇒ self
Run the MongoDB update to commit the changes.
-
#success? ⇒ Boolean
Whether the operation is completed and was successful.
-
#total ⇒ Integer
Total number of units to be captured.
Constructor Details
#initialize(sku, available, backordered) ⇒ Capture
Returns a new instance of Capture.
10 11 12 13 14 |
# File 'app/models/workarea/inventory/capture.rb', line 10 def initialize(sku, available, backordered) @sku = sku @available = available @backordered = backordered end |
Instance Attribute Details
#result ⇒ Object (readonly)
Returns the value of attribute result.
8 9 10 |
# File 'app/models/workarea/inventory/capture.rb', line 8 def result @result end |
Instance Method Details
#perform ⇒ self
Run the MongoDB update to commit the changes. Uses MongoDB’s findAndModify command.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/models/workarea/inventory/capture.rb', line 39 def perform return self if @result.present? assert_availability find_and_modify! @result = { success: success?, available: success? ? @available : 0, backordered: success? ? @backordered : 0, backordered_until: @sku.backordered_until } self end |
#success? ⇒ Boolean
Whether the operation is completed and was successful. Returns false if the #perform method hasn’t been called.
29 30 31 32 |
# File 'app/models/workarea/inventory/capture.rb', line 29 def success? !!(@database_result && @database_result.available == capture_attributes[:available]) end |
#total ⇒ Integer
Total number of units to be captured.
20 21 22 |
# File 'app/models/workarea/inventory/capture.rb', line 20 def total @available + @backordered end |