module Returnly module Discounts class LineItem attr_reader :adjustment, :order def initialize(order, adjustment) @adjustment = adjustment @order = order end def discount_amount(line_item, units = 0) return 0.0 if units <= 0 return 0.0 if adjustment.adjustable != line_item units = line_item.quantity if units > line_item.quantity adjustment.amount * weight_of(line_item, units.to_d) end private def weight_of(line_item, units) (units / line_item.quantity).round(2, :down) end end end end