1
0

Implement a rule base class

Implement a potential base class for future rules
This commit is contained in:
Tim Kächele 2021-03-07 19:02:34 +01:00
parent 337419fe28
commit 1e0b8db8b3

View File

@ -0,0 +1,51 @@
# frozen_string_literal: true
module PlanetExpress
class Rule
attr_reader :discount_budget_for_month_repository,
:shipping_option_repository,
:shipment_repository
def initialize(discount_budget_for_month_repository:,
shipping_option_repository:,
shipment_repository: )
@discount_budget_for_month_repository = discount_budget_for_month_repository
@shipping_option_repository = shipping_option_repository
@shipment_repository = shipment_repository
end
def apply_to(shipment)
return unless discount_budget(shipment).remaining_budget?
return unless applicable_to?(shipment)
register_discount(shipment, discount_for(shipment))
end
def inspect
self.class.name
end
protected
def register_discount(shipment, discount)
shipment.add_discount(discount)
discount_budget(shipment).register_applied_discount(discount.amount)
end
def applicable_to?(shipment)
raise NotImplementedError
end
def discount_for(shipment)
raise NotImplementedError
end
def price(shipment)
shipment.shipping_option.price_in_cents
end
def discount_budget(shipment)
discount_budget_for_month_repository.for_month(shipment.date)
end
end
end