1
0

Optimize provider_and_package_size lookup

This commit is contained in:
Tim Kächele 2021-11-20 20:47:28 +01:00
parent 9db9d55985
commit 31b0cbd896

View File

@ -6,10 +6,11 @@ module PlanetExpress
def initialize(data: nil) def initialize(data: nil)
@data = data || default_data @data = data || default_data
calculate_provider_size_lookup_table
end end
def find_by_provider_and_package_size(provider, package_size) def find_by_provider_and_package_size(provider, package_size)
where(provider: provider, package_size: package_size).first provider_size_lookup_table[[provider, package_size]]&.first
end end
def lowest_price_for_package_size(package_size) def lowest_price_for_package_size(package_size)
@ -17,6 +18,7 @@ module PlanetExpress
end end
private private
attr_reader :provider_size_lookup_table
def default_data def default_data
[ [
@ -28,5 +30,11 @@ module PlanetExpress
ShippingOption.new('MR', 'L', 400) ShippingOption.new('MR', 'L', 400)
] ]
end end
def calculate_provider_size_lookup_table
@provider_size_lookup_table = data.group_by do |option|
[option.provider, option.package_size]
end
end
end end
end end