Class: OrderOptimizer::Sku

Inherits:
Object
  • Object
show all
Defined in:
lib/order_optimizer/sku.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, quantity:, price_per_sku: nil, price_per_unit: nil, min_quantity: nil, max_quantity: nil) ⇒ Sku

Returns a new instance of Sku.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/order_optimizer/sku.rb', line 5

def initialize(id, quantity:, price_per_sku: nil, price_per_unit: nil, min_quantity: nil, max_quantity: nil)
  @id = id
  @quantity = BigDecimal(quantity, 2)
  @min_quantity = BigDecimal(min_quantity, 2) if min_quantity
  @max_quantity = BigDecimal(max_quantity, 2) if max_quantity

  if min_quantity && max_quantity && (min_quantity > max_quantity)
    raise ArgumentError, "min_quantity can't be larger than max_quantity"
  end

  raise ArgumentError, ':price_per_sku or :price_per_unit must be set' unless price_per_unit || price_per_sku

  @price_per_unit = BigDecimal(price_per_unit, 2) if price_per_unit
  @price_per_unit ||= BigDecimal(price_per_sku, 2) / quantity

  @price_per_sku = BigDecimal(price_per_sku,  2) if price_per_sku
  @price_per_sku ||= quantity * price_per_unit
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/order_optimizer/sku.rb', line 3

def id
  @id
end

#max_quantityObject (readonly)

Returns the value of attribute max_quantity.



3
4
5
# File 'lib/order_optimizer/sku.rb', line 3

def max_quantity
  @max_quantity
end

#min_quantityObject (readonly)

Returns the value of attribute min_quantity.



3
4
5
# File 'lib/order_optimizer/sku.rb', line 3

def min_quantity
  @min_quantity
end

#price_per_skuObject (readonly)

Returns the value of attribute price_per_sku.



3
4
5
# File 'lib/order_optimizer/sku.rb', line 3

def price_per_sku
  @price_per_sku
end

#price_per_unitObject (readonly)

Returns the value of attribute price_per_unit.



3
4
5
# File 'lib/order_optimizer/sku.rb', line 3

def price_per_unit
  @price_per_unit
end

#quantityObject (readonly)

Returns the value of attribute quantity.



3
4
5
# File 'lib/order_optimizer/sku.rb', line 3

def quantity
  @quantity
end