Class: Workarea::Inventory::Capture

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#resultObject (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

#performself

Run the MongoDB update to commit the changes. Uses MongoDB’s findAndModify command.

Returns:

  • (self)


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.

Returns:

  • (Boolean)


29
30
31
32
# File 'app/models/workarea/inventory/capture.rb', line 29

def success?
  !!(@database_result &&
       @database_result.available == capture_attributes[:available])
end

#totalInteger

Total number of units to be captured.

Returns:

  • (Integer)


20
21
22
# File 'app/models/workarea/inventory/capture.rb', line 20

def total
  @available + @backordered
end