Class: WorkDay

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/work_day.rb,
lib/work_day/version.rb

Constant Summary collapse

VERSION =
'2.0.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.calendars(new_calendars) ⇒ Object

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



61
62
63
# File 'lib/work_day.rb', line 61

def calendars(new_calendars)
  self.calendars = new_calendars
end

Instance Method Details

#between(start_date, end_date) ⇒ Object



44
45
46
# 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

#calendarsObject



10
11
12
# File 'lib/work_day.rb', line 10

def calendars
  Thread.current[:work_day_calendars]
end

#calendars=(new_calendars) ⇒ Object



14
15
16
# File 'lib/work_day.rb', line 14

def calendars=(new_calendars)
  Thread.current[:work_day_calendars] = new_calendars
end

#last_until(date, days = 0) ⇒ Object



25
26
27
28
29
30
31
32
# 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) ⇒ Object



34
35
36
37
38
39
40
41
42
# 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) ⇒ Object



48
49
50
51
52
53
54
# 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) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
# 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