Skip to content

Commit

Permalink
Add RegulatedCosts class
Browse files Browse the repository at this point in the history
  • Loading branch information
javierav committed Jan 30, 2024
1 parent 2c63a63 commit b8a5995
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Style/Documentation:
Enabled: false
Style/FrozenStringLiteralComment:
EnforcedStyle: never
Style/HashTransformValues:
Exclude:
- "**/app/lib/regulated_costs.rb"
Style/OpenStructUse:
Exclude:
- "**/test/**/*"
Expand Down
2 changes: 2 additions & 0 deletions app/lib/energy_period.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class EnergyPeriod
PERIODS = %i[peak standard off_peak].freeze

def self.for(time)
new(time).for
end
Expand Down
48 changes: 48 additions & 0 deletions app/lib/regulated_costs.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b8a5995

Please sign in to comment.