1
0

Optimize faster l shipment count lookup

This commit is contained in:
Tim Kächele 2021-11-20 19:02:28 +01:00
parent 72c160b894
commit 7b6606e32b

View File

@ -2,6 +2,16 @@
module PlanetExpress module PlanetExpress
class ShipmentRepository < Repository class ShipmentRepository < Repository
attr_reader :valid_l_shipments_by_month
def initialize(data:)
@data = []
@valid_l_shipments_by_month = Hash.new { |hash, key| hash[key] = [] }
data.each do |entry|
add(entry)
end
end
def rule_applied_in_month?(month, rule) def rule_applied_in_month?(month, rule)
valid_shipments.any? do |shipment| valid_shipments.any? do |shipment|
shipment.month == month && shipment.month == month &&
@ -10,13 +20,20 @@ module PlanetExpress
end end
def l_shipment_count_for_provider_and_month(provider, month) def l_shipment_count_for_provider_and_month(provider, month)
valid_shipments.select do |shipment| valid_l_shipments_by_month[month].select do |shipment|
shipment.month == month && shipment.provider == provider
shipment.l_shipment? &&
shipment.provider == provider
end.length end.length
end end
def add(item)
data.push(item)
item.if_valid do
if item.l_shipment?
valid_l_shipments_by_month[item.month].push(item)
end
end
end
private private
def valid_shipments def valid_shipments