Module: WorkDays::CalculationMethods

Included in:
HolidayMethods, WorkSchedules::Bank, WorkSchedules::Default
Defined in:
lib/work_days/calculation_methods.rb

Instance Method Summary collapse

Instance Method Details

#holiday?(date) ⇒ Boolean

Returns:

  • (Boolean)


2
3
4
5
6
7
# File 'lib/work_days/calculation_methods.rb', line 2

def holiday?(date)
  observed_holidays.any? do |sym|
    next if caller.any?{|c| c =~ /`#{sym.to_s}'\z/}
    send(sym, date.year) == date
  end
end

#holidays_in_range(start, stop) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/work_days/calculation_methods.rb', line 35

def holidays_in_range(start, stop)
  holidays = []

  (start.to_date..stop.to_date).each do |date|
    holidays << date if holiday?(date)
  end

  holidays
end

#monthly_work_days(year = nil) ⇒ Object



82
83
84
85
86
# File 'lib/work_days/calculation_methods.rb', line 82

def monthly_work_days(year=nil)
  year = format_year(year)

  (1..12).collect{|month| work_days_in_month(Date.new(year, month))}
end

#next_work_day(date) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/work_days/calculation_methods.rb', line 61

def next_work_day(date)
  loop do
    date = date.to_date.next_day
    break if work_day?(date)
  end

  date
end

#non_work_day?(date) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/work_days/calculation_methods.rb', line 21

def non_work_day?(date)
  !work_day?(date)
end

#observed_holidaysObject

Raises:

  • (NotImplementedError)


78
79
80
# File 'lib/work_days/calculation_methods.rb', line 78

def observed_holidays
  raise NotImplementedError, 'You must override this method.'
end

#previous_work_day(date) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/work_days/calculation_methods.rb', line 52

def previous_work_day(date)
  loop do
    date = date.to_date.prev_day
    break if work_day?(date)
  end

  date
end

#week_day?(date) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/work_days/calculation_methods.rb', line 13

def week_day?(date)
  !weekend_day?(date)
end

#weekend_day?(date) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/work_days/calculation_methods.rb', line 9

def weekend_day?(date)
  date.sunday? || date.saturday?
end

#work_day?(date) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/work_days/calculation_methods.rb', line 17

def work_day?(date)
  week_day?(date) && !holiday?(date)
end

#work_days_from(number_of_days, date) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/work_days/calculation_methods.rb', line 70

def work_days_from(number_of_days, date)
  number_of_days.times do
    date = next_work_day(date)
  end

  date
end

#work_days_in_month(date) ⇒ Object



45
46
47
48
49
50
# File 'lib/work_days/calculation_methods.rb', line 45

def work_days_in_month(date)
  start_date = Date.new(date.year, date.month,  1)
  end_date   = Date.new(date.year, date.month, -1)

  work_days_in_range(start_date, end_date)
end

#work_days_in_range(start, stop) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/work_days/calculation_methods.rb', line 25

def work_days_in_range(start, stop)
  working_days = []

  (start.to_date..stop.to_date).each do |date|
    working_days << date if work_day?(date)
  end

  working_days
end