Sha256: 46e2a7388f3abd31bccc7bbb7f79020fdf1fca30825fd56b5fc775e9030b6ae6

Contents?: true

Size: 932 Bytes

Versions: 1

Compression:

Stored size: 932 Bytes

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
work_day-0.3.1 lib/work_day.rb