class WorkDay

Constants

VERSION

Public Class Methods

calendars(new_calendars) click to toggle source

TODO: remove in favor of calendars=. Requires major version upgrade.

# File lib/work_day.rb, line 61
def calendars(new_calendars)
  self.calendars = new_calendars
end

Public Instance Methods

between(start_date, end_date) click to toggle source
# File lib/work_day.rb, line 44
def between(start_date, end_date)
  (start_date.to_date..end_date.to_date).select(&method(:work_day?))
end
calendars() click to toggle source
# File lib/work_day.rb, line 10
def calendars
  Thread.current[:work_day_calendars]
end
calendars=(new_calendars) click to toggle source
# File lib/work_day.rb, line 14
def calendars=(new_calendars)
  Thread.current[:work_day_calendars] = new_calendars
end
last_until(date, days = 0) click to toggle source
# File lib/work_day.rb, line 25
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
next_after(date, days = 1) click to toggle source
# File lib/work_day.rb, line 34
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
use_calendars(*new_calendars) { || ... } click to toggle source
# File lib/work_day.rb, line 48
def use_calendars(*new_calendars)
  old_calendars = calendars
  self.calendars = new_calendars
  yield
ensure
  self.calendars = old_calendars
end
work_day?(date) click to toggle source
# File lib/work_day.rb, line 18
def work_day?(date)
  date = date.try(:to_date)
  return unless date

  ![0, 6].include?(date.wday) && !holidays_in(date.year).include?(date)
end

Private Instance Methods

holidays_in(year) click to toggle source
# File lib/work_day.rb, line 68
def holidays_in(year)
  self.calendars ||= ['brazil']
  @holidays ||= {}
  @holidays[calendars] ||= {}
  @holidays[calendars][year] ||= Holiday.new(calendars).in(year)
end