class FastBusinessTime::HolidayCollection

Attributes

schedule[R]

Public Class Methods

new(collection:, schedule:) click to toggle source
# File lib/fast_business_time/holiday_collection.rb, line 4
def initialize(collection:, schedule:)
  @schedule = schedule
  @dates = calculate_holidays_time(collection)
end

Public Instance Methods

days_in_date_range(first_date, second_date) click to toggle source
# File lib/fast_business_time/holiday_collection.rb, line 9
def days_in_date_range(first_date, second_date)
  holidays_before(second_date + 1) - holidays_before(first_date)
end
include?(date) click to toggle source
# File lib/fast_business_time/holiday_collection.rb, line 17
def include?(date)
  get(date)[:holiday?]
end
seconds_in_date_range(first_date, second_date) click to toggle source
# File lib/fast_business_time/holiday_collection.rb, line 13
def seconds_in_date_range(first_date, second_date)
  holiday_seconds_before(second_date + 1) - holiday_seconds_before(first_date)
end

Private Instance Methods

calculate_holidays_time(collection) click to toggle source
# File lib/fast_business_time/holiday_collection.rb, line 37
def calculate_holidays_time(collection)
  return {} if collection.empty?

  holidays_before = 0
  holiday_seconds_before = 0

  sorted_holidays = SortedSet.new(collection)
  first_holiday = sorted_holidays.min
  last_holiday = sorted_holidays.max

  (first_holiday..last_holiday).each_with_object({}) do |date, hash|
    holiday = sorted_holidays.include?(date)
    working_seconds = schedule.seconds_per_wday(date.wday)
    hash[date] = {
      holiday?: holiday,
      holidays_before: holidays_before,
      holiday_seconds_before: holiday_seconds_before
    }
    if holiday && working_seconds > 0
      holidays_before += 1
      holiday_seconds_before += working_seconds
    end
  end
end
get(date) click to toggle source
# File lib/fast_business_time/holiday_collection.rb, line 33
def get(date)
  @dates[date] || { holiday?: false, holidays_before: 0, holiday_seconds_before: 0 }
end
holiday_seconds_before(date) click to toggle source
# File lib/fast_business_time/holiday_collection.rb, line 29
def holiday_seconds_before(date)
  get(date)[:holiday_seconds_before]
end
holidays_before(date) click to toggle source
# File lib/fast_business_time/holiday_collection.rb, line 25
def holidays_before(date)
  get(date)[:holidays_before]
end