class BusinessCalendar::HolidayDeterminer

Constants

DEFAULT_TIME_TO_LIVE

Attributes

additions[R]
additions_only[R]
holiday_names[R]
regions[R]
removals[R]

Public Class Methods

new(regions, holiday_names, opts = {}) click to toggle source
# File lib/business_calendar/holiday_determiner.rb, line 7
def initialize(regions, holiday_names, opts = {})
  ttl = opts[:ttl]
  @time_to_live = ttl.nil? ? DEFAULT_TIME_TO_LIVE : ttl
  @regions        = regions
  @holiday_names  = holiday_names
  @additions      = opts[:additions]      || []
  @removals       = opts[:removals]       || []
  @additions_only = opts[:additions_only] || false
end

Public Instance Methods

call(date) click to toggle source
# File lib/business_calendar/holiday_determiner.rb, line 17
def call(date)
  clear_cache if should_clear_cache?

  if additions.include? date
    true
  elsif removals.include? date
    false
  elsif !additions_only
    Holidays.between(date, date, @regions, :observed).
      any? { |h| @holiday_names.include? h[:name] }
  end
end

Private Instance Methods

clear_cache() click to toggle source
# File lib/business_calendar/holiday_determiner.rb, line 38
def clear_cache
  @last_cleared = Time.now
  @additions_cache = nil
  @removals_cache = nil
end
should_clear_cache?() click to toggle source
# File lib/business_calendar/holiday_determiner.rb, line 32
def should_clear_cache?
  return false unless @time_to_live

  !@last_cleared || (Time.now - @last_cleared) >= @time_to_live
end