Implement a rule base class
Implement a potential base class for future rules
This commit is contained in:
parent
337419fe28
commit
1e0b8db8b3
51
lib/planet_express/rule.rb
Normal file
51
lib/planet_express/rule.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user