From b8a5995ab2715f93248689deb338226f16fc6c26 Mon Sep 17 00:00:00 2001 From: Javier Aranda Date: Tue, 30 Jan 2024 20:10:22 +0100 Subject: [PATCH] Add RegulatedCosts class --- .rubocop.yml | 3 +++ app/lib/energy_period.rb | 2 ++ app/lib/regulated_costs.rb | 48 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 app/lib/regulated_costs.rb diff --git a/.rubocop.yml b/.rubocop.yml index 6527dd9..4efcba3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -56,6 +56,9 @@ Style/Documentation: Enabled: false Style/FrozenStringLiteralComment: EnforcedStyle: never +Style/HashTransformValues: + Exclude: + - "**/app/lib/regulated_costs.rb" Style/OpenStructUse: Exclude: - "**/test/**/*" diff --git a/app/lib/energy_period.rb b/app/lib/energy_period.rb index f7fefd5..166c541 100644 --- a/app/lib/energy_period.rb +++ b/app/lib/energy_period.rb @@ -1,4 +1,6 @@ class EnergyPeriod + PERIODS = %i[peak standard off_peak].freeze + def self.for(time) new(time).for end diff --git a/app/lib/regulated_costs.rb b/app/lib/regulated_costs.rb new file mode 100644 index 0000000..8fe83aa --- /dev/null +++ b/app/lib/regulated_costs.rb @@ -0,0 +1,48 @@ +class RegulatedCosts + COSTS = %i[transport distribution charges].freeze + + def self.for(time_or_date) + new(time_or_date).for + end + + def initialize(time_or_date) + @time_or_date = time_or_date + end + + def for + if @time_or_date.is_a?(Date) + for_date + elsif @time_or_date.is_a?(Time) + for_time + end + end + + private + + def for_date + costs[@time_or_date.year.to_s] + end + + def for_time + for_date[EnergyPeriod.for(@time_or_date)] + end + + def prices + # [year, [[peak_transport, peak_distribution, peak_charges], [standard...], [off_peak...]]] + [ + ["2023", [[0.004506, 0.024592, 0.043893], [0.003050, 0.016744, 0.008779], [0.000128, 0.000852, 0.002195]]], + ["2024", [[0.004528, 0.028553, 0.043893], [0.002589, 0.016595, 0.008779], [0.000075, 0.000482, 0.002195]]] + ] + end + + def costs + prices.to_h do |year, periods| + [ + year, + EnergyPeriod::PERIODS.each_with_index.to_h do |period, period_index| + [period, COSTS.each_with_index.to_h { |type, type_index| [type, periods[period_index][type_index]] }] + end + ] + end + end +end