Module: Spok::Workday

Defined in:
lib/spok/workday.rb

Overview

Public: Various methods useful for checking for restdays and workdays. All methods are module methods and should be called on the Spok::Workday module.

Constant Summary collapse

WEEKDAYS =

Public: Hash containing all weekdays.

{
  sunday: 0,
  monday: 1,
  tuesday: 2,
  wednesday: 3,
  thrusday: 4,
  friday: 5,
  saturday: 6
}.freeze

Class Method Summary collapse

Class Method Details

.holiday?(date, calendar: Spok.default_calendar) ⇒ Boolean

Public: Checks if a given Date is on a holiday.

date - The Date to be checked. calendar - Symbol informing in which calendar the date will be checked

(default: :brazil).

Examples

Spok::Workday.holiday?(Date.new(2012, 5, 1))
# => true

Returns a boolean.

Returns:

  • (Boolean)

80
81
82
83
# File 'lib/spok/workday.rb', line 80

def self.holiday?(date, calendar: Spok.default_calendar)
  dates = Spok::Calendars.get(calendar)
  dates.include?(date.to_date)
end

.last_workday(date, calendar: Spok.default_calendar) ⇒ Object

Public: Returns the last workday until the informed date. It returns the informed date in case it is a workday.

date - End Date to check for workdays. calendar - Symbol informing in which calendar to check for workdays

(default: :brazil).

Examples

Spok::Workday.last_workday(Date.new(2012, 10, 21))
# => #<Date: 2012-10-19 ((2456220j,0s,0n),+0s,2299161j)>

95
96
97
98
99
# File 'lib/spok/workday.rb', line 95

def self.last_workday(date, calendar: Spok.default_calendar)
  return date if workday?(date, calendar: calendar)

  last_workday((date - 1.day), calendar: calendar)
end

.next_workday(date, calendar: Spok.default_calendar) ⇒ Object

Public: Returns the next workday starting from the informed date. It returns the informed date in case it is a workday.

date - Start Date to check for workdays. calendar - Symbol informing in which calendar to check for workdays

(default: :brazil).

Examples

Spok::Workday.next_workday(Date.new(2012, 10, 21))
# => #<Date: 2012-10-19 ((2456220j,0s,0n),+0s,2299161j)>

111
112
113
114
115
# File 'lib/spok/workday.rb', line 111

def self.next_workday(date, calendar: Spok.default_calendar)
  return date if workday?(date, calendar: calendar)

  next_workday((date + 1.day), calendar: calendar)
end

.restday?(date, calendar: Spok.default_calendar) ⇒ Boolean

Public: Checks if a given day is a restday.

date - The Date to be checked. calendar - Symbol informing in which calendar the date will be checked

(default: :brazil).

Examples

Spok::Workday.restday?(Date.new(2012, 8, 6))
# => false

Returns a boolean.

Returns:

  • (Boolean)

32
33
34
# File 'lib/spok/workday.rb', line 32

def self.restday?(date, calendar: Spok.default_calendar)
  self.weekend?(date) || self.holiday?(date, calendar: calendar)
end

.weekend?(date) ⇒ Boolean

Public: Checks if a given Date is on a weekend.

date - The Date to be checked.

Examples

Spok::Workday.weekend?(Date.new(2012, 8, 6))
# => false

Returns a boolean.

Returns:

  • (Boolean)

62
63
64
65
66
# File 'lib/spok/workday.rb', line 62

def self.weekend?(date)
  weekday = date.wday

  [WEEKDAYS[:saturday], WEEKDAYS[:sunday]].include? weekday
end

.workday?(date, calendar: Spok.default_calendar) ⇒ Boolean

Public: Checks if a given day is a workday.

date - The Date to be checked. calendar - Symbol informing in which calendar the date will be checked

(default: :brazil).

Examples

Spok::Workday.workday?(Date.new(2012, 8, 6))
# => true

Returns a boolean.

Returns:

  • (Boolean)

48
49
50
# File 'lib/spok/workday.rb', line 48

def self.workday?(date, calendar: Spok.default_calendar)
  !restday?(date, calendar: calendar)
end