module WorkingDays
This module contains logic for working_days_until
, weekday?
, and public_holiday?
.
Constants
- HOLIDAYS
TODO: could we use github.com/alphagov/gds-api-adapters ?
- WEEK_DAYS
Public Class Methods
check_lookup()
click to toggle source
# File lib/ndr_support/concerns/working_days.rb, line 150 def self.check_lookup return true if HOLIDAYS.max >= 1.year.from_now warn "NdrSupport's WorkingDays extension has under a year of future data. Check for updates?" false end
Public Instance Methods
public_holiday?()
click to toggle source
Is this a public holiday (in England / Wales)?
# File lib/ndr_support/concerns/working_days.rb, line 180 def public_holiday? HOLIDAYS.include? to_date end
weekday?()
click to toggle source
Is this a weekday?
# File lib/ndr_support/concerns/working_days.rb, line 175 def weekday? WEEK_DAYS.include? wday end
weekdays_until(other)
click to toggle source
How many complete weekdays there are until the given ‘other`. Returns negative number if `other` is earlier.
# File lib/ndr_support/concerns/working_days.rb, line 169 def weekdays_until(other) return -other.weekdays_until(self) if other < self count_whole_days_to(other, &:weekday?) end
working_days_until(other)
click to toggle source
How many complete working days there are until the given ‘other`. Returns negative number if `other` is earlier.
# File lib/ndr_support/concerns/working_days.rb, line 159 def working_days_until(other) return -other.working_days_until(self) if other < self count_whole_days_to(other) do |day| day.weekday? && !day.public_holiday? end end
Private Instance Methods
count_whole_days_to(other, &block)
click to toggle source
# File lib/ndr_support/concerns/working_days.rb, line 186 def count_whole_days_to(other, &block) day = self + 1.day count = 0 while day <= other count += 1 if block.call(day) day += 1.day end count end