Optimize DiscountBudget for month lookups
This commit is contained in:
parent
c6b32d66c7
commit
ce4075a507
@ -6,29 +6,16 @@ module PlanetExpress
|
||||
attr_reader :data
|
||||
|
||||
def initialize(data: {})
|
||||
@data = data
|
||||
@data = Hash.new do |hash, key|
|
||||
hash[key] = DiscountBudget.new(budget: DEFAULT_BUDGET)
|
||||
end.merge(data)
|
||||
end
|
||||
|
||||
def for_month(date)
|
||||
return budget_for_month(date) if budget_for_month?(date)
|
||||
|
||||
add_budget_for_month(date)
|
||||
budget_for_month(date)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def budget_for_month(date)
|
||||
data[start_of_month(date)]
|
||||
end
|
||||
|
||||
def budget_for_month?(date)
|
||||
!budget_for_month(date).nil?
|
||||
end
|
||||
|
||||
def add_budget_for_month(date)
|
||||
data[start_of_month(date)] = DiscountBudget.new(budget: DEFAULT_BUDGET)
|
||||
end
|
||||
private
|
||||
|
||||
def start_of_month(date)
|
||||
Date.new(date.year, date.month, 1)
|
||||
|
Loading…
Reference in New Issue
Block a user