In my Ruby on Rails app I have this model:
class Car < ApplicationRecord
attr_accessor :name, :brand, :origin_country
module ShippingCostCalculatorForJapan
def calculate
# Details left out for brevity
end
# A bunch of other expensive methods
end
module ShippingCostCalculatorForGermany
def calculate
# Details left out for brevity
end
# A bunch of other expensive methods
end
end
How can I make it that only the relevant module gets loaded?
For example if I have a car
object with origin_country
“Japan”, only the module ShippingCostCalculatorForJapan
should be applied.
How can this be done or what might be a better (= best practice) design pattern to achieve this?
As it stands now, I have a bunch of different CostCalculator
modules all containing the exact same methods but with different values in them. The values are hardcoded to the respective country and can’t be changed.
I could also easily re-organize those modules into classes. What would be the best approach here?