class Holidays::Definition::Repository::Cache

Public Class Methods

new() click to toggle source
# File lib/holidays/definition/repository/cache.rb, line 5
def initialize
  reset!
end

Public Instance Methods

cache_between(start_date, end_date, cache_data, options) click to toggle source
# File lib/holidays/definition/repository/cache.rb, line 9
def cache_between(start_date, end_date, cache_data, options)
  raise ArgumentError unless cache_data
  raise ArgumentError unless start_date && end_date

  @cache_range[options] = start_date..end_date
  @cache[options] = cache_data.group_by { |holiday| holiday[:date] }
end
find(start_date, end_date, options) click to toggle source
# File lib/holidays/definition/repository/cache.rb, line 17
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!() click to toggle source
# File lib/holidays/definition/repository/cache.rb, line 29
def reset!
  @cache = {}
  @cache_range = {}
end

Private Instance Methods

in_cache_range?(start_date, end_date, options) click to toggle source
# File lib/holidays/definition/repository/cache.rb, line 36
def in_cache_range?(start_date, end_date, options)
  range = @cache_range[options]
  if range
    range.begin <= start_date && range.end >= end_date
  else
    false
  end
end