Class: HolidayCo::HolidayCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/holiday_co/holiday_calculator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year) ⇒ HolidayCalculator

Returns a new instance of HolidayCalculator.



20
21
22
# File 'lib/holiday_co/holiday_calculator.rb', line 20

def initialize(year)
  @year = year
end

Instance Attribute Details

#yearObject (readonly)

Returns the value of attribute year.



11
12
13
# File 'lib/holiday_co/holiday_calculator.rb', line 11

def year
  @year
end

Class Method Details

.for(year) ⇒ Object



13
14
15
16
17
18
# File 'lib/holiday_co/holiday_calculator.rb', line 13

def self.for(year)
  year = year.to_i
  return new(year).calculate unless HolidayCo.cache_enabled?

  HolidayCo.cache.fetch(year) { new(year).calculate }
end

Instance Method Details

#calculateObject

Results are deep-frozen: they are shared through the cache across callers and threads, so they must be immutable.



26
27
28
29
30
31
32
# File 'lib/holiday_co/holiday_calculator.rb', line 26

def calculate
  [fixed_holidays, pascua_holidays, movable_holidays]
    .flatten
    .sort_by! { |h| h[:date] }
    .each { |h| h[:date].freeze; h.freeze }
    .freeze
end