Class: BusinessCalendar::HolidayDeterminer

Inherits:
Object
  • Object
show all
Defined in:
lib/business_calendar/holiday_determiner.rb

Constant Summary collapse

DEFAULT_TIME_TO_LIVE =
24 * 60 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regions, holiday_names, opts = {}) ⇒ HolidayDeterminer

Returns a new instance of HolidayDeterminer.



7
8
9
10
11
12
13
14
15
# 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

Instance Attribute Details

#additions_onlyObject (readonly)

Returns the value of attribute additions_only.



5
6
7
# File 'lib/business_calendar/holiday_determiner.rb', line 5

def additions_only
  @additions_only
end

#holiday_namesObject (readonly)

Returns the value of attribute holiday_names.



5
6
7
# File 'lib/business_calendar/holiday_determiner.rb', line 5

def holiday_names
  @holiday_names
end

#regionsObject (readonly)

Returns the value of attribute regions.



5
6
7
# File 'lib/business_calendar/holiday_determiner.rb', line 5

def regions
  @regions
end

Instance Method Details

#additionsObject



30
31
32
# File 'lib/business_calendar/holiday_determiner.rb', line 30

def additions
  @additions_cache ||= @additions.is_a?(Proc) ? @additions.call : @additions
end

#call(date) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# 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

#removalsObject



34
35
36
# File 'lib/business_calendar/holiday_determiner.rb', line 34

def removals
  @removals_cache ||= @removals.is_a?(Proc) ? @removals.call : @removals
end