require "holiday" require "active_support" require "active_support/core_ext" module WorkDay @holidays = {} def self.work_day?(date) if date = date.try(:to_date) ![0, 6].include?(date.wday) && !holidays_in(date.year).include?(date) end end def self.last_until(date, days = 0) if date = date.try(:to_date) while !work_day?(date) date -= 1 end days -= 1 days == -1 ? date : last_until(1.day.ago(date), days) end end def self.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 self.between(start_date, end_date) (start_date.to_date..end_date.to_date).select { |date| work_day?(date) } end private def self.holidays_in(year) @holidays[year] ||= Holiday.in(year) end end