Skip to content

Commit

Permalink
Merge pull request #82 from mattmcf/master
Browse files Browse the repository at this point in the history
Save calendar name reference in loaded calendar
  • Loading branch information
JoeSouthan authored Mar 4, 2021
2 parents 6c100d9 + c260dbb commit 6daebf7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
14 changes: 9 additions & 5 deletions lib/business/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def self.calendar_directories
end
private_class_method :calendar_directories

# rubocop:disable Metrics/MethodLength
def self.load(calendar_name)
data = find_calendar_data(calendar_name)
raise "No such calendar '#{calendar_name}'" unless data
Expand All @@ -25,11 +26,13 @@ def self.load(calendar_name)
end

new(
name: calendar_name,
holidays: data["holidays"],
working_days: data["working_days"],
extra_working_dates: data["extra_working_dates"],
)
end
# rubocop:enable Metrics/MethodLength

def self.find_calendar_data(calendar_name)
calendar_directories.detect do |path|
Expand All @@ -54,12 +57,13 @@ def self.load_cached(calendar)

DAY_NAMES = %( mon tue wed thu fri sat sun )

attr_reader :holidays, :working_days, :extra_working_dates
attr_reader :name, :holidays, :working_days, :extra_working_dates

def initialize(config)
set_extra_working_dates(config[:extra_working_dates])
set_working_days(config[:working_days])
set_holidays(config[:holidays])
def initialize(name:, extra_working_dates: nil, working_days: nil, holidays: nil)
@name = name
set_extra_working_dates(extra_working_dates)
set_working_days(working_days)
set_holidays(holidays)

unless (@holidays & @extra_working_dates).none?
raise ArgumentError, "Holidays cannot be extra working dates"
Expand Down
39 changes: 24 additions & 15 deletions spec/business/calendar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
describe "#set_working_days" do
subject(:set_working_days) { calendar.set_working_days(working_days) }

let(:calendar) { described_class.new({}) }
let(:calendar) { described_class.new(name: "test") }
let(:working_days) { [] }

context "when given valid working days" do
Expand Down Expand Up @@ -123,7 +123,7 @@
describe "#set_holidays" do
subject(:holidays) { calendar.holidays }

let(:calendar) { described_class.new({}) }
let(:calendar) { described_class.new(name: "test") }
let(:holiday_dates) { [] }

before { calendar.set_holidays(holiday_dates) }
Expand All @@ -148,7 +148,7 @@
describe "#set_extra_working_dates" do
subject(:extra_working_dates) { calendar.extra_working_dates }

let(:calendar) { described_class.new({}) }
let(:calendar) { described_class.new(name: "test") }
let(:extra_dates) { [] }

before { calendar.set_extra_working_dates(extra_dates) }
Expand All @@ -172,7 +172,8 @@

context "when holiday is also a working date" do
let(:instance) do
described_class.new(holidays: ["2018-01-06"],
described_class.new(name: "test",
holidays: ["2018-01-06"],
extra_working_dates: ["2018-01-06"])
end

Expand All @@ -184,7 +185,8 @@

context "when working date on working day" do
let(:instance) do
described_class.new(working_days: ["mon"],
described_class.new(name: "test",
working_days: ["mon"],
extra_working_dates: ["Monday 26th Mar, 2018"])
end

Expand All @@ -202,7 +204,8 @@
subject { calendar.business_day?(day) }

let(:calendar) do
described_class.new(holidays: ["9am, Tuesday 1st Jan, 2013"],
described_class.new(name: "test",
holidays: ["9am, Tuesday 1st Jan, 2013"],
extra_working_dates: ["9am, Sunday 6th Jan, 2013"])
end

Expand Down Expand Up @@ -235,7 +238,8 @@
subject { calendar.working_day?(day) }

let(:calendar) do
described_class.new(holidays: ["9am, Tuesday 1st Jan, 2013"],
described_class.new(name: "test",
holidays: ["9am, Tuesday 1st Jan, 2013"],
extra_working_dates: ["9am, Sunday 6th Jan, 2013"])
end

Expand Down Expand Up @@ -268,7 +272,8 @@
subject { calendar.holiday?(day) }

let(:calendar) do
described_class.new(holidays: ["9am, Tuesday 1st Jan, 2013"],
described_class.new(name: "test",
holidays: ["9am, Tuesday 1st Jan, 2013"],
extra_working_dates: ["9am, Sunday 6th Jan, 2013"])
end

Expand Down Expand Up @@ -301,7 +306,7 @@
subject { calendar.roll_forward(date) }

let(:calendar) do
described_class.new(holidays: ["Tuesday 1st Jan, 2013"])
described_class.new(name: "test", holidays: ["Tuesday 1st Jan, 2013"])
end

context "given a business day" do
Expand Down Expand Up @@ -329,7 +334,7 @@
subject { calendar.roll_backward(date) }

let(:calendar) do
described_class.new(holidays: ["Tuesday 1st Jan, 2013"])
described_class.new(name: "test", holidays: ["Tuesday 1st Jan, 2013"])
end

context "given a business day" do
Expand Down Expand Up @@ -357,7 +362,7 @@
subject { calendar.next_business_day(date) }

let(:calendar) do
described_class.new(holidays: ["Tuesday 1st Jan, 2013"])
described_class.new(name: "test", holidays: ["Tuesday 1st Jan, 2013"])
end

context "given a business day" do
Expand Down Expand Up @@ -385,7 +390,7 @@
subject { calendar.previous_business_day(date) }

let(:calendar) do
described_class.new(holidays: ["Tuesday 1st Jan, 2013"])
described_class.new(name: "test", holidays: ["Tuesday 1st Jan, 2013"])
end

context "given a business day" do
Expand Down Expand Up @@ -414,7 +419,8 @@

let(:extra_working_dates) { [] }
let(:calendar) do
described_class.new(holidays: ["Tuesday 1st Jan, 2013"],
described_class.new(name: "test",
holidays: ["Tuesday 1st Jan, 2013"],
extra_working_dates: extra_working_dates)
end
let(:delta) { 2 }
Expand Down Expand Up @@ -458,7 +464,8 @@

let(:extra_working_dates) { [] }
let(:calendar) do
described_class.new(holidays: ["Thursday 3rd Jan, 2013"],
described_class.new(name: "test",
holidays: ["Thursday 3rd Jan, 2013"],
extra_working_dates: extra_working_dates)
end
let(:delta) { 2 }
Expand Down Expand Up @@ -511,7 +518,9 @@
["Sun 1/6/2014", "Sat 28/6/2014", "Sat 5/7/2014"]
end
let(:calendar) do
described_class.new(holidays: holidays, extra_working_dates: extra_working_dates)
described_class.new(name: "test",
holidays: holidays,
extra_working_dates: extra_working_dates)
end

context "starting on a business day" do
Expand Down

0 comments on commit 6daebf7

Please sign in to comment.