Skip to content

Commit

Permalink
Consider breaks in in_hours? calculation
Browse files Browse the repository at this point in the history
Since the calculation behind `in_hours?` is optimized to avoid period
generation, we need to take extra steps to ensure it takes breaks into
consideration.

Closes #69.
  • Loading branch information
craiglittle committed Jun 9, 2016
1 parent ddfd6b2 commit 1bbbf60
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/biz/calculation/active.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def initialize(schedule, time)
end

def result
schedule.intervals.any? { |interval| interval.contains?(time) } &&
schedule.intervals.any? { |interval| interval.contains?(time) } &&
schedule.breaks.none? { |break_| break_.contains?(time) } &&
schedule.holidays.none? { |holiday| holiday.contains?(time) }
end

Expand Down
28 changes: 25 additions & 3 deletions spec/calculation/active_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
RSpec.describe Biz::Calculation::Active do
subject(:calculation) {
described_class.new(schedule(holidays: [Date.new(2006, 1, 4)]), time)
described_class.new(
schedule(
breaks: {Date.new(2006, 1, 3) => {'05:30' => '11:00'}},
holidays: [Date.new(2006, 1, 4)]
),
time
)
}

describe '#result' do
context 'when the time is not contained by an interval' do
context 'and not on a holiday' do
context 'and not on a break nor holiday' do
let(:time) { Time.utc(2006, 1, 1, 6) }

it 'returns false' do
expect(calculation.result).to eq false
end
end

context 'and on a break' do
let(:time) { Time.utc(2006, 1, 3, 6) }

it 'returns false' do
expect(calculation.result).to eq false
end
end

context 'and on a holiday' do
let(:time) { Time.utc(2006, 1, 4, 6) }

Expand All @@ -23,14 +37,22 @@
end

context 'when the time is contained by an interval' do
context 'and not on a holiday' do
context 'and not on a break nor a holiday' do
let(:time) { Time.utc(2006, 1, 2, 12) }

it 'returns true' do
expect(calculation.result).to eq true
end
end

context 'and on a break' do
let(:time) { Time.utc(2006, 1, 3, 10) }

it 'returns false' do
expect(calculation.result).to eq false
end
end

context 'and on a holiday' do
let(:time) { Time.utc(2006, 1, 4, 12) }

Expand Down
4 changes: 2 additions & 2 deletions spec/schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
end

context 'when the time is in business hours' do
let(:time) { Time.utc(2006, 1, 2, 10) }
let(:time) { Time.utc(2006, 1, 2, 12) }

it 'returns true' do
expect(schedule.in_hours?(time)).to eq true
Expand All @@ -122,7 +122,7 @@
end

context 'when the time is in business hours' do
let(:time) { Time.utc(2006, 1, 2, 10) }
let(:time) { Time.utc(2006, 1, 2, 12) }

it 'returns true' do
expect(schedule.business_hours?(time)).to eq true
Expand Down

0 comments on commit 1bbbf60

Please sign in to comment.