1
0

Optimize rule_applied_in_month? lookup

This commit is contained in:
Tim Kächele 2021-11-20 19:40:07 +01:00
parent 7b6606e32b
commit 9db9d55985

View File

@ -2,19 +2,19 @@
module PlanetExpress
class ShipmentRepository < Repository
attr_reader :valid_l_shipments_by_month
attr_reader :valid_l_shipments_by_month, :valid_shipments_by_month
def initialize(data:)
@data = []
@valid_l_shipments_by_month = Hash.new { |hash, key| hash[key] = [] }
@valid_shipments_by_month = Hash.new { |hash, key| hash[key] = [] }
data.each do |entry|
add(entry)
end
end
def rule_applied_in_month?(month, rule)
valid_shipments.any? do |shipment|
shipment.month == month &&
valid_shipments_by_month[month].any? do |shipment|
shipment.discounts.any? { |discount| discount.rule.instance_of?(rule) }
end
end
@ -28,22 +28,11 @@ module PlanetExpress
def add(item)
data.push(item)
item.if_valid do
valid_shipments_by_month[item.month].push(item)
if item.l_shipment?
valid_l_shipments_by_month[item.month].push(item)
end
end
end
private
def valid_shipments
valid_shipments = []
data.each do |shipment|
shipment.if_valid do |valid_shipment|
valid_shipments.push(valid_shipment)
end
end
valid_shipments
end
end
end