Class: OrderOptimizer::Order
- Inherits:
-
Object
- Object
- OrderOptimizer::Order
- Defined in:
- lib/order_optimizer/order.rb
Instance Attribute Summary collapse
-
#quantity ⇒ Object
readonly
Returns the value of attribute quantity.
-
#skus ⇒ Object
readonly
Returns the value of attribute skus.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Instance Method Summary collapse
- #add(sku, count: 1) ⇒ Object
- #complete? ⇒ Boolean
- #exact? ⇒ Boolean
-
#initialize(required_qty:) ⇒ Order
constructor
A new instance of Order.
- #missing_qty ⇒ Object
Constructor Details
#initialize(required_qty:) ⇒ Order
Returns a new instance of Order.
5 6 7 8 9 10 |
# File 'lib/order_optimizer/order.rb', line 5 def initialize(required_qty:) @quantity = 0 @required_qty = required_qty @total = 0 @skus = {} end |
Instance Attribute Details
#quantity ⇒ Object (readonly)
Returns the value of attribute quantity.
3 4 5 |
# File 'lib/order_optimizer/order.rb', line 3 def quantity @quantity end |
#skus ⇒ Object (readonly)
Returns the value of attribute skus.
3 4 5 |
# File 'lib/order_optimizer/order.rb', line 3 def skus @skus end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
3 4 5 |
# File 'lib/order_optimizer/order.rb', line 3 def total @total end |
Instance Method Details
#add(sku, count: 1) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/order_optimizer/order.rb', line 12 def add(sku, count: 1) @quantity += count * sku.quantity @total += count * sku.price_per_sku @skus = skus.merge(sku.id => count) { |_identifier, current, plus| current + plus } self end |
#complete? ⇒ Boolean
23 24 25 |
# File 'lib/order_optimizer/order.rb', line 23 def complete? missing_qty.zero? end |
#exact? ⇒ Boolean
27 28 29 |
# File 'lib/order_optimizer/order.rb', line 27 def exact? @required_qty == quantity end |
#missing_qty ⇒ Object
19 20 21 |
# File 'lib/order_optimizer/order.rb', line 19 def missing_qty [@required_qty - quantity, 0].max end |