Sha256: 1510228421e900beeebe43ec3615c4342cda1ed31ebf71c59433ceccb608f6ef

Contents?: true

Size: 989 Bytes

Versions: 3

Compression:

Stored size: 989 Bytes

Contents

module HolidaysFromGoogleCalendar
  class CacheUnit
    attr_reader :holidays, :date_min, :date_max

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

    def size
      @holidays.size
    end

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

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

    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

    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
holidays_from_google_calendar-1.0.0 lib/holidays_from_google_calendar/cache_unit.rb
holidays_from_google_calendar-0.4.7 lib/holidays_from_google_calendar/cache_unit.rb
holidays_from_google_calendar-0.4.6 lib/holidays_from_google_calendar/cache_unit.rb