Module: Spok::Workday
- Defined in:
- lib/spok/workday.rb
Overview
Internal: 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 =
Internal: Hash containing all weekdays.
{ sunday: 0, monday: 1, tuesday: 2, wednesday: 3, thrusday: 4, friday: 5, saturday: 6 }.freeze
Class Method Summary collapse
-
.holiday?(date, calendar: Spok.default_calendar) ⇒ Boolean
Internal: Checks if a given Date is on a holiday.
-
.last_workday(date, calendar: Spok.default_calendar) ⇒ Object
Internal: Returns the last workday until the informed date.
-
.next_workday(date, calendar: Spok.default_calendar) ⇒ Object
Internal: Returns the next workday starting from the informed date.
-
.restday?(date, calendar: Spok.default_calendar) ⇒ Boolean
Internal: Checks if a given day is a restday.
-
.weekend?(date) ⇒ Boolean
Internal: Checks if a given Date is on a weekend.
-
.workday?(date, calendar: Spok.default_calendar) ⇒ Boolean
Internal: Checks if a given day is a workday.
Class Method Details
.holiday?(date, calendar: Spok.default_calendar) ⇒ Boolean
Internal: 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.
81 82 83 84 |
# File 'lib/spok/workday.rb', line 81 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
Internal: 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)>
96 97 98 99 100 |
# File 'lib/spok/workday.rb', line 96 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
Internal: 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)>
112 113 114 115 116 |
# File 'lib/spok/workday.rb', line 112 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
Internal: 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.
33 34 35 |
# File 'lib/spok/workday.rb', line 33 def self.restday?(date, calendar: Spok.default_calendar) self.weekend?(date) || self.holiday?(date, calendar: calendar) end |
.weekend?(date) ⇒ Boolean
Internal: 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.
63 64 65 66 67 |
# File 'lib/spok/workday.rb', line 63 def self.weekend?(date) weekday = date.wday [WEEKDAYS[:saturday], WEEKDAYS[:sunday]].include? weekday end |
.workday?(date, calendar: Spok.default_calendar) ⇒ Boolean
Internal: 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.
49 50 51 |
# File 'lib/spok/workday.rb', line 49 def self.workday?(date, calendar: Spok.default_calendar) !restday?(date, calendar: calendar) end |