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