From 81788e0cd779f53d73203a82f008b61cdf658dbc Mon Sep 17 00:00:00 2001 From: Jason Dugdale Date: Tue, 20 Oct 2015 00:12:10 +0100 Subject: [PATCH 1/4] Failing test cases for issues #36, #45 and #83 --- test/test_business_days.rb | 40 +++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/test/test_business_days.rb b/test/test_business_days.rb index 0e1bff6..fe08b84 100644 --- a/test/test_business_days.rb +++ b/test/test_business_days.rb @@ -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 From 812482e9e3bbcd67243b121540bf424a6508e557 Mon Sep 17 00:00:00 2001 From: Jason Dugdale Date: Tue, 20 Oct 2015 00:13:24 +0100 Subject: [PATCH 2/4] Resolution attempt for issues #36, #45 and #83. Ensure we wind to beginning of business day when using business_hours before/after methods --- lib/business_time/business_days.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/business_time/business_days.rb b/lib/business_time/business_days.rb index d330ed5..2ae8126 100644 --- a/lib/business_time/business_days.rb +++ b/lib/business_time/business_days.rb @@ -4,7 +4,7 @@ module BusinessTime class BusinessDays include Comparable attr_reader :days - + def initialize(days) @days = days end @@ -15,13 +15,18 @@ def <=>(other) 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 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 @@ -34,9 +39,16 @@ def before(time = Time.current) days -= 1 if time.workday? time = 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 From 99cdb1ecd01c3c4476817f73a0033f897097a7cd Mon Sep 17 00:00:00 2001 From: Jason Dugdale Date: Tue, 20 Oct 2015 00:16:09 +0100 Subject: [PATCH 3/4] Trivial refactoring suggested by Rubocop --- lib/business_time/business_days.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/business_time/business_days.rb b/lib/business_time/business_days.rb index 2ae8126..e2bc102 100644 --- a/lib/business_time/business_days.rb +++ b/lib/business_time/business_days.rb @@ -11,7 +11,7 @@ def initialize(days) 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 @@ -20,7 +20,7 @@ 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 @@ -37,7 +37,7 @@ 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 From ed889d94865d22d4c8a87acf51a43cde94112601 Mon Sep 17 00:00:00 2001 From: Jason Dugdale Date: Tue, 20 Oct 2015 10:25:07 +0100 Subject: [PATCH 4/4] Enable tests to run on new (much faster) Travis architecture --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 1764b09..267c091 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: ruby +sudo: false rvm: - 1.9.3 - 2.0.0