Class: HolidaysFromGoogleCalendar::CacheUnit

Inherits:
Object
  • Object
show all
Defined in:
lib/holidays_from_google_calendar/cache_unit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(holidays, date_min, date_max) ⇒ CacheUnit

Returns a new instance of CacheUnit.



5
6
7
8
9
# File 'lib/holidays_from_google_calendar/cache_unit.rb', line 5

def initialize(holidays, date_min, date_max)
  @holidays = holidays
  @date_min = date_min
  @date_max = date_max
end

Instance Attribute Details

#date_maxObject (readonly)

Returns the value of attribute date_max.



3
4
5
# File 'lib/holidays_from_google_calendar/cache_unit.rb', line 3

def date_max
  @date_max
end

#date_minObject (readonly)

Returns the value of attribute date_min.



3
4
5
# File 'lib/holidays_from_google_calendar/cache_unit.rb', line 3

def date_min
  @date_min
end

#holidaysObject (readonly)

Returns the value of attribute holidays.



3
4
5
# File 'lib/holidays_from_google_calendar/cache_unit.rb', line 3

def holidays
  @holidays
end

Instance Method Details

#combine(other) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/holidays_from_google_calendar/cache_unit.rb', line 28

def combine(other)
  return unless overlapped?(other)
  @date_min = [@date_min, other.date_min].min
  @date_max = [@date_max, other.date_max].max
  @holidays =
    @holidays.concat(other.holidays).uniq.sort { |a, b| a.date <=> b.date }
end

#include?(date_min, date_max) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/holidays_from_google_calendar/cache_unit.rb', line 15

def include?(date_min, date_max)
  [date_min, date_max].all? { |e| @date_min <= e && e <= @date_max }
end

#overlapped?(other) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/holidays_from_google_calendar/cache_unit.rb', line 23

def overlapped?(other)
  (@date_min <= other.date_max && other.date_min <= @date_max + 1.day) ||
    (other.date_min <= @date_max && @date_min <= other.date_max + 1.day)
end

#retrieve(date_min, date_max) ⇒ Object



19
20
21
# File 'lib/holidays_from_google_calendar/cache_unit.rb', line 19

def retrieve(date_min, date_max)
  @holidays.select { |e| date_min <= e.date && e.date <= date_max }
end

#sizeObject



11
12
13
# File 'lib/holidays_from_google_calendar/cache_unit.rb', line 11

def size
  @holidays.size
end