Skip to content

Commit

Permalink
Merge pull request #120 from dugjason/feature/issue-83
Browse files Browse the repository at this point in the history
Fixes for issues #36, #45 and #83 (bug in business_days.before/after)
  • Loading branch information
bokmann committed Feb 3, 2016
2 parents 96191e3 + ed889d9 commit 731dac8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: ruby
sudo: false
rvm:
- 1.9.3
- 2.0.0
Expand Down
24 changes: 18 additions & 6 deletions lib/business_time/business_days.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@ module BusinessTime
class BusinessDays
include Comparable
attr_reader :days

def initialize(days)
@days = days
end

def <=>(other)
if other.class != self.class
raise ArgumentError.new("#{self.class.to_s} can't be compared with #{other.class.to_s}")
raise ArgumentError.new("#{self.class} can't be compared with #{other.class}")
end
self.days <=> other.days
end

def after(time = Time.current)
days = @days
while days > 0 || !time.workday?
days -= 1 if time.workday?
time = time + 1.day
time += 1.day
end
# If we have a Time or DateTime object, we can roll_forward to the
# beginning of the next business day
if time.is_a?(Time) || time.is_a?(DateTime)
time = Time.roll_forward(time) unless time.during_business_hours?
end
time
end
Expand All @@ -32,11 +37,18 @@ def before(time = Time.current)
days = @days
while days > 0 || !time.workday?
days -= 1 if time.workday?
time = time - 1.day
time -= 1.day
end
# If we have a Time or DateTime object, we can roll_backward to the
# beginning of the previous business day
if time.is_a?(Time) || time.is_a?(DateTime)
unless time.during_business_hours?
time = Time.beginning_of_workday(Time.roll_backward(time))
end
end
time
end

alias_method :ago, :before
alias_method :until, :before
end
Expand Down
40 changes: 35 additions & 5 deletions test/test_business_days.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,57 @@
expected = Time.parse("April 8th, 2010, 11:00 am")
assert_equal expected, before
end


it "should return a business hour when adding one business day from before business hours" do
wednesday = Time.parse("Wednesday October 14th, 2015, 01:54 am")
later = 1.business_days.after(wednesday)
expected = Time.parse("Thursday October 15th, 2015, 09:00 am")
assert_equal expected, later
end

it "should return a business hour when adding one business day from after business hours" do
wednesday = Time.parse("Wednesday October 14th, 2015, 21:54 pm")
later = 1.business_days.after(wednesday)
expected = Time.parse("Friday October 16th, 2015, 09:00 am")
assert_equal expected, later
end

it "should return a business hour when subtracting one business day from before business hours" do
wednesday = Time.parse("Wednesday October 14th, 2015, 01:54 am")
before = 1.business_days.before(wednesday)
expected = Time.parse("Monday October 12th, 2015, 09:00 am")
assert before.during_business_hours?
assert_equal expected, before
end

it "should return a business hour when subtracting one business day from after business hours" do
wednesday = Time.parse("Wednesday October 14th, 2015, 21:54 pm")
before = 1.business_days.before(wednesday)
expected = Time.parse("Tuesday October 13th, 2015, 09:00 am")
assert before.during_business_hours?
assert_equal expected, before
end

it "responds appropriatly to <" do
assert 5.business_days < 10.business_days
assert !(10.business_days < 5.business_days)
end

it "responds appropriatly to >" do
assert !(5.business_days > 10.business_days)
assert 10.business_days > 5.business_days
end

it "responds appropriatly to ==" do
assert 5.business_days == 5.business_days
assert 10.business_days != 5.business_days
end

it "won't compare days to hours" do
assert_raises ArgumentError do
5.business_days < 5.business_hours
end
end

end
end

0 comments on commit 731dac8

Please sign in to comment.