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 periods
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 6256838 commit 382e51b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 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
26 changes: 24 additions & 2 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 @@ -31,6 +45,14 @@
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

0 comments on commit 382e51b

Please sign in to comment.