# frozen_string_literal: true require 'holiday' require 'active_support' require 'active_support/core_ext' class WorkDay include Singleton def calendars Thread.current[:work_day_calendars] end def calendars=(new_calendars) Thread.current[:work_day_calendars] = new_calendars end def work_day?(date) date = date.try(:to_date) return unless date ![0, 6].include?(date.wday) && !holidays_in(date.year).include?(date) end def last_until(date, days = 0) date = date.try(:to_date) return unless date date -= 1 until work_day?(date) days -= 1 days == -1 ? date : last_until(1.day.ago(date), days) end def next_after(date, days = 1) date = date.try(:to_date) day_counter = 0 while day_counter < days date += 1 day_counter += 1 if work_day?(date) end work_day?(date) ? date : next_after(date) end def between(start_date, end_date) (start_date.to_date..end_date.to_date).select(&method(:work_day?)) end def use_calendars(*new_calendars) old_calendars = calendars self.calendars = new_calendars yield ensure self.calendars = old_calendars end class << self delegate :work_day?, :last_until, :next_after, :between, :calendars=, :use_calendars, to: :instance # TODO: remove in favor of #calendars=. Requires major version upgrade. def calendars(new_calendars) self.calendars = new_calendars end end private def holidays_in(year) self.calendars ||= ['brazil'] @holidays ||= {} @holidays[calendars] ||= {} @holidays[calendars][year] ||= Holiday.new(calendars).in(year) end end