Skip to content

Commit

Permalink
Merge pull request #56 from gocardless/add-seperate-methods-for-holid…
Browse files Browse the repository at this point in the history
…ay-and-working-day

Add separate methods for determining if a date is a working_day / holiday
  • Loading branch information
Andrew Morton authored Jun 8, 2020
2 parents b7bcef4 + d46c920 commit 624341f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.0 - June 8, 2020

- Add seperate `working_day?` and `holiday?` methods to the calendar

## 2.0.0 - May 4, 2020

🚨 **BREAKING CHANGES** 🚨
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ calendar.business_day?(Date.parse("Sunday, 8 June 2014"))
# => false
```

More specifically you can check if a given `business_day?` is either a `working_day?` or a `holiday?` using methods on `Business::Calendar`.

```ruby
# Assuming "Monday, 9 June 2014" is a holiday
calendar.working_day?(Date.parse("Monday, 9 June 2014"))
# => true
calendar.holiday?(Date.parse("Monday, 9 June 2014"))
# => true
# Monday is a working day, but we have a holiday so it's not
# a business day
calendar.business_day?(Date.parse("Monday, 9 June 2014"))
# => false
```

## Business day arithmetic

The `add_business_days` and `subtract_business_days` are used to perform business day arithmetic on dates.
Expand Down
20 changes: 14 additions & 6 deletions lib/business/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,26 @@ def initialize(config)
set_extra_working_dates(config[:extra_working_dates])
set_working_days(config[:working_days])
set_holidays(config[:holidays])

unless (@holidays & @extra_working_dates).none?
raise ArgumentError, 'Holidays cannot be extra working dates'
end
end

# Return true if the date given is a business day (typically that means a
# non-weekend day) and not a holiday.
def business_day?(date)
date = date.to_date
return true if extra_working_dates.include?(date)
return false unless working_days.include?(date.strftime('%a').downcase)
return false if holidays.include?(date)
true
working_day?(date) && !holiday?(date)
end

def working_day?(date)
date = date.to_date
extra_working_dates.include?(date) || working_days.include?(date.strftime('%a').downcase)
end

def holiday?(date)
holidays.include?(date.to_date)
end

# Roll forward to the next business day. If the date given is a business
Expand Down Expand Up @@ -191,8 +201,6 @@ def parse_dates(dates)
# Internal method for assigning holidays from a calendar config.
def set_holidays(holidays)
@holidays = parse_dates(holidays)
return if (@holidays & @extra_working_dates).none?
raise ArgumentError, 'Holidays cannot be extra working dates'
end

def set_extra_working_dates(extra_working_dates)
Expand Down
2 changes: 1 addition & 1 deletion lib/business/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Business
VERSION = "2.0.0"
VERSION = "2.1.0"
end
56 changes: 56 additions & 0 deletions spec/calendar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,62 @@
end
end

describe "#working_day?" do
let(:calendar) do
Business::Calendar.new(holidays: ["9am, Tuesday 1st Jan, 2013"],
extra_working_dates: ["9am, Sunday 6th Jan, 2013"])
end
subject { calendar.working_day?(day) }

context "when given a working day" do
let(:day) { date_class.parse("9am, Wednesday 2nd Jan, 2013") }
it { is_expected.to be_truthy }
end

context "when given a non-working day" do
let(:day) { date_class.parse("9am, Saturday 5th Jan, 2013") }
it { is_expected.to be_falsey }
end

context "when given a working day that is a holiday" do
let(:day) { date_class.parse("9am, Tuesday 1st Jan, 2013") }
it { is_expected.to be_truthy }
end

context "when given a non-business day that is a working date" do
let(:day) { date_class.parse("9am, Sunday 6th Jan, 2013") }
it { is_expected.to be_truthy }
end
end

describe "#holiday?" do
let(:calendar) do
Business::Calendar.new(holidays: ["9am, Tuesday 1st Jan, 2013"],
extra_working_dates: ["9am, Sunday 6th Jan, 2013"])
end
subject { calendar.holiday?(day) }

context "when given a working day that is not a holiday" do
let(:day) { date_class.parse("9am, Wednesday 2nd Jan, 2013") }
it { is_expected.to be_falsey }
end

context "when given a non-working day that is not a holiday day" do
let(:day) { date_class.parse("9am, Saturday 5th Jan, 2013") }
it { is_expected.to be_falsey }
end

context "when given a day that is a holiday" do
let(:day) { date_class.parse("9am, Tuesday 1st Jan, 2013") }
it { is_expected.to be_truthy }
end

context "when given a non-business day that is no a holiday" do
let(:day) { date_class.parse("9am, Sunday 6th Jan, 2013") }
it { is_expected.to be_falsey }
end
end

describe "#roll_forward" do
let(:calendar) do
Business::Calendar.new(holidays: ["Tuesday 1st Jan, 2013"])
Expand Down

0 comments on commit 624341f

Please sign in to comment.