Skip to content

Commit 6daebf7

Browse files
authored
Merge pull request #82 from mattmcf/master
Save calendar name reference in loaded calendar
2 parents 6c100d9 + c260dbb commit 6daebf7

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

lib/business/calendar.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def self.calendar_directories
1616
end
1717
private_class_method :calendar_directories
1818

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

2728
new(
29+
name: calendar_name,
2830
holidays: data["holidays"],
2931
working_days: data["working_days"],
3032
extra_working_dates: data["extra_working_dates"],
3133
)
3234
end
35+
# rubocop:enable Metrics/MethodLength
3336

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

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

57-
attr_reader :holidays, :working_days, :extra_working_dates
60+
attr_reader :name, :holidays, :working_days, :extra_working_dates
5861

59-
def initialize(config)
60-
set_extra_working_dates(config[:extra_working_dates])
61-
set_working_days(config[:working_days])
62-
set_holidays(config[:holidays])
62+
def initialize(name:, extra_working_dates: nil, working_days: nil, holidays: nil)
63+
@name = name
64+
set_extra_working_dates(extra_working_dates)
65+
set_working_days(working_days)
66+
set_holidays(holidays)
6367

6468
unless (@holidays & @extra_working_dates).none?
6569
raise ArgumentError, "Holidays cannot be extra working dates"

spec/business/calendar_spec.rb

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
describe "#set_working_days" do
8585
subject(:set_working_days) { calendar.set_working_days(working_days) }
8686

87-
let(:calendar) { described_class.new({}) }
87+
let(:calendar) { described_class.new(name: "test") }
8888
let(:working_days) { [] }
8989

9090
context "when given valid working days" do
@@ -123,7 +123,7 @@
123123
describe "#set_holidays" do
124124
subject(:holidays) { calendar.holidays }
125125

126-
let(:calendar) { described_class.new({}) }
126+
let(:calendar) { described_class.new(name: "test") }
127127
let(:holiday_dates) { [] }
128128

129129
before { calendar.set_holidays(holiday_dates) }
@@ -148,7 +148,7 @@
148148
describe "#set_extra_working_dates" do
149149
subject(:extra_working_dates) { calendar.extra_working_dates }
150150

151-
let(:calendar) { described_class.new({}) }
151+
let(:calendar) { described_class.new(name: "test") }
152152
let(:extra_dates) { [] }
153153

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

173173
context "when holiday is also a working date" do
174174
let(:instance) do
175-
described_class.new(holidays: ["2018-01-06"],
175+
described_class.new(name: "test",
176+
holidays: ["2018-01-06"],
176177
extra_working_dates: ["2018-01-06"])
177178
end
178179

@@ -184,7 +185,8 @@
184185

185186
context "when working date on working day" do
186187
let(:instance) do
187-
described_class.new(working_days: ["mon"],
188+
described_class.new(name: "test",
189+
working_days: ["mon"],
188190
extra_working_dates: ["Monday 26th Mar, 2018"])
189191
end
190192

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

204206
let(:calendar) do
205-
described_class.new(holidays: ["9am, Tuesday 1st Jan, 2013"],
207+
described_class.new(name: "test",
208+
holidays: ["9am, Tuesday 1st Jan, 2013"],
206209
extra_working_dates: ["9am, Sunday 6th Jan, 2013"])
207210
end
208211

@@ -235,7 +238,8 @@
235238
subject { calendar.working_day?(day) }
236239

237240
let(:calendar) do
238-
described_class.new(holidays: ["9am, Tuesday 1st Jan, 2013"],
241+
described_class.new(name: "test",
242+
holidays: ["9am, Tuesday 1st Jan, 2013"],
239243
extra_working_dates: ["9am, Sunday 6th Jan, 2013"])
240244
end
241245

@@ -268,7 +272,8 @@
268272
subject { calendar.holiday?(day) }
269273

270274
let(:calendar) do
271-
described_class.new(holidays: ["9am, Tuesday 1st Jan, 2013"],
275+
described_class.new(name: "test",
276+
holidays: ["9am, Tuesday 1st Jan, 2013"],
272277
extra_working_dates: ["9am, Sunday 6th Jan, 2013"])
273278
end
274279

@@ -301,7 +306,7 @@
301306
subject { calendar.roll_forward(date) }
302307

303308
let(:calendar) do
304-
described_class.new(holidays: ["Tuesday 1st Jan, 2013"])
309+
described_class.new(name: "test", holidays: ["Tuesday 1st Jan, 2013"])
305310
end
306311

307312
context "given a business day" do
@@ -329,7 +334,7 @@
329334
subject { calendar.roll_backward(date) }
330335

331336
let(:calendar) do
332-
described_class.new(holidays: ["Tuesday 1st Jan, 2013"])
337+
described_class.new(name: "test", holidays: ["Tuesday 1st Jan, 2013"])
333338
end
334339

335340
context "given a business day" do
@@ -357,7 +362,7 @@
357362
subject { calendar.next_business_day(date) }
358363

359364
let(:calendar) do
360-
described_class.new(holidays: ["Tuesday 1st Jan, 2013"])
365+
described_class.new(name: "test", holidays: ["Tuesday 1st Jan, 2013"])
361366
end
362367

363368
context "given a business day" do
@@ -385,7 +390,7 @@
385390
subject { calendar.previous_business_day(date) }
386391

387392
let(:calendar) do
388-
described_class.new(holidays: ["Tuesday 1st Jan, 2013"])
393+
described_class.new(name: "test", holidays: ["Tuesday 1st Jan, 2013"])
389394
end
390395

391396
context "given a business day" do
@@ -414,7 +419,8 @@
414419

415420
let(:extra_working_dates) { [] }
416421
let(:calendar) do
417-
described_class.new(holidays: ["Tuesday 1st Jan, 2013"],
422+
described_class.new(name: "test",
423+
holidays: ["Tuesday 1st Jan, 2013"],
418424
extra_working_dates: extra_working_dates)
419425
end
420426
let(:delta) { 2 }
@@ -458,7 +464,8 @@
458464

459465
let(:extra_working_dates) { [] }
460466
let(:calendar) do
461-
described_class.new(holidays: ["Thursday 3rd Jan, 2013"],
467+
described_class.new(name: "test",
468+
holidays: ["Thursday 3rd Jan, 2013"],
462469
extra_working_dates: extra_working_dates)
463470
end
464471
let(:delta) { 2 }
@@ -511,7 +518,9 @@
511518
["Sun 1/6/2014", "Sat 28/6/2014", "Sat 5/7/2014"]
512519
end
513520
let(:calendar) do
514-
described_class.new(holidays: holidays, extra_working_dates: extra_working_dates)
521+
described_class.new(name: "test",
522+
holidays: holidays,
523+
extra_working_dates: extra_working_dates)
515524
end
516525

517526
context "starting on a business day" do

0 commit comments

Comments
 (0)