Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RegulatedCosts class #99

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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