Sha256: 255e5f83e6240647c3082bc021416e5faa8ea4f0de2e8bd6e599faec91c4e951

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

# 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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
work_day-2.0.1 lib/work_day.rb
work_day-2.0.0 lib/work_day.rb
work_day-1.3.1 lib/work_day.rb
work_day-1.3.0 lib/work_day.rb
work_day-1.2.0 lib/work_day.rb