Sha256: 25f8406127e6a3110b02782e5cae8d7c4ceb3fd2b5a08bef7610e4b0b9323143

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

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) ||
        (other.date_min <= date_max && date_min <= other.date_max)
    end

    def combine(other)
      return unless overlapped?(other)

      if date_min <= other.date_max
        date_min = other.date_min # rubocop:disable Lint/UselessAssignment
      elsif other.date_min <= date_max
        date_max = other.date_max # rubocop:disable Lint/UselessAssignment
      end
      @holidays =
        @holidays.concat(other.holidays).uniq.sort { |a, b| a.date <=> b.date }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
holidays_from_google_calendar-0.4.4 lib/holidays_from_google_calendar/cache_unit.rb
holidays_from_google_calendar-0.4.3 lib/holidays_from_google_calendar/cache_unit.rb
holidays_from_google_calendar-0.4.2 lib/holidays_from_google_calendar/cache_unit.rb
holidays_from_google_calendar-0.4.1 lib/holidays_from_google_calendar/cache_unit.rb
holidays_from_google_calendar-0.4.0 lib/holidays_from_google_calendar/cache_unit.rb