Class: Holidays::Definition::Repository::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/holidays/definition/repository/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



5
6
7
# File 'lib/holidays/definition/repository/cache.rb', line 5

def initialize
  reset!
end

Instance Method Details

#cache_between(start_date, end_date, cache_data, options) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
# File 'lib/holidays/definition/repository/cache.rb', line 9

def cache_between(start_date, end_date, cache_data, options)
  raise ArgumentError unless cache_data

  @cache_range[options] = start_date..end_date
  @cache[options] = cache_data.group_by { |holiday| holiday[:date] }
end

#find(start_date, end_date, options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/holidays/definition/repository/cache.rb', line 16

def find(start_date, end_date, options)
  return nil unless in_cache_range?(start_date, end_date, options)
  
  if start_date == end_date
    @cache[options].fetch(start_date, [])
  else
    @cache[options].select do |date, holidays|
      date >= start_date && date <= end_date
    end.flat_map { |date, holidays| holidays }
  end
end

#reset!Object



28
29
30
31
# File 'lib/holidays/definition/repository/cache.rb', line 28

def reset!
  @cache = {}
  @cache_range = {}
end