From f71e2040d664b420c7674867670347455bee9da5 Mon Sep 17 00:00:00 2001 From: kennyeni Date: Tue, 19 May 2015 22:02:19 -0500 Subject: [PATCH 1/4] Add compatibility for time_select TOD currently works with time_field out of the box, but this HTML field is not supported in all browsers, so this adds compatibility for a helper method that generates standard HTML5 fields --- lib/tod/time_of_day.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/tod/time_of_day.rb b/lib/tod/time_of_day.rb index 4707820..95ac455 100644 --- a/lib/tod/time_of_day.rb +++ b/lib/tod/time_of_day.rb @@ -2,7 +2,7 @@ module Tod class TimeOfDay include Comparable - attr_reader :hour, :minute, :second, :second_of_day + attr_reader :hour, :minute, :second, :second_of_day, :day, :month, :year alias_method :min, :minute alias_method :sec, :second alias_method :to_i, :second_of_day @@ -46,6 +46,9 @@ def initialize(h, m=0, s=0) @hour = Integer(h) @minute = Integer(m) @second = Integer(s) + @day = Integer(1) + @month = Integer(1) + @year = Integer(2000) raise ArgumentError, "hour must be between 0 and 23" unless (0..23).include?(@hour) raise ArgumentError, "minute must be between 0 and 59" unless (0..59).include?(@minute) From 63ca0153a42480cba0e01e0db6556fc94b3b6fba Mon Sep 17 00:00:00 2001 From: kennyeni Date: Fri, 29 May 2015 23:38:38 -0500 Subject: [PATCH 2/4] Support for overlapping Tod when day changes --- Gemfile.lock | 2 +- lib/tod/shift.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index ce1afdf..98980ae 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - tod (2.0.0) + tod (2.0.1) GEM remote: https://rubygems.org/ diff --git a/lib/tod/shift.rb b/lib/tod/shift.rb index 4cee77c..71b6c77 100644 --- a/lib/tod/shift.rb +++ b/lib/tod/shift.rb @@ -33,7 +33,7 @@ def include?(tod) # Returns true if ranges overlap, false otherwise. def overlaps?(other) - a, b = [self, other].map(&:range).sort_by(&:first) + a, b = [self, other].map(&:range) op = a.exclude_end? ? :> : :>= a.last.send(op, b.first) end From e052c96462b92ed2004fb6545874d67fab04ded5 Mon Sep 17 00:00:00 2001 From: kennyeni Date: Sat, 30 May 2015 03:03:30 -0500 Subject: [PATCH 3/4] Support for shifts that span to other days --- lib/tod/shift.rb | 15 ++++++++++++++- test/tod/shift_test.rb | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/tod/shift.rb b/lib/tod/shift.rb index 71b6c77..233f172 100644 --- a/lib/tod/shift.rb +++ b/lib/tod/shift.rb @@ -33,9 +33,22 @@ def include?(tod) # Returns true if ranges overlap, false otherwise. def overlaps?(other) + max_seconds = TimeOfDay::NUM_SECONDS_IN_DAY + + # Standard case a, b = [self, other].map(&:range) + a, b = [a, b].sort_by(&:first) op = a.exclude_end? ? :> : :>= - a.last.send(op, b.first) + return true if a.last.send(op, b.first) + + # Special cases + if (a.last > max_seconds) || (b.last > max_seconds) + a = Range.new(a.first, a.last - max_seconds, a.exclude_end?) if a.last > max_seconds + b = Range.new(b.first, b.last - max_seconds, b.exclude_end?) if b.last > max_seconds + a, b = [a, b].sort_by(&:last) + b.last.send(op, a.last) && a.last.send(op, b.first) + end + end def contains?(shift) diff --git a/test/tod/shift_test.rb b/test/tod/shift_test.rb index 0a1f7e0..384959b 100644 --- a/test/tod/shift_test.rb +++ b/test/tod/shift_test.rb @@ -43,12 +43,49 @@ shift1 = Tod::Shift.new(Tod::TimeOfDay.new(12), Tod::TimeOfDay.new(18)) shift2 = Tod::Shift.new(Tod::TimeOfDay.new(13), Tod::TimeOfDay.new(15)) assert shift1.overlaps?(shift2) + + # Additional Testing for shifts that Range from one day to another + cases = [ + [5, 8, 7, 2], + [7, 2, 1, 8], + [7, 2, 5, 8], + [4, 8, 1, 5], + [1, 5, 4, 8], + [7, 2, 1, 4], + [1, 4, 7, 2], + [1, 4, 3, 2], + [5, 8, 7, 2], + [7, 2, 8, 3], + [7, 2, 6, 3], + [7, 2, 1, 8] + ] + + cases.each do |c| + shift1 = Tod::Shift.new(Tod::TimeOfDay.new(c[0]), Tod::TimeOfDay.new(c[1])) + shift2 = Tod::Shift.new(Tod::TimeOfDay.new(c[2]), Tod::TimeOfDay.new(c[3])) + assert shift1.overlaps?(shift2), "Failed with args: #{c}" + end end it "is false when shifts don't overlap" do shift1 = Tod::Shift.new(Tod::TimeOfDay.new(1), Tod::TimeOfDay.new(5)) shift2 = Tod::Shift.new(Tod::TimeOfDay.new(9), Tod::TimeOfDay.new(12)) refute shift1.overlaps?(shift2) + + # Additional Testing for shifts that Range from one day to another + cases = [ + [7, 8, 1, 5], + [1, 5, 7, 8], + [7, 2, 3, 4], + [3, 4, 5, 2], + [1, 5, 9, 12] + ] + + cases.each do |c| + shift1 = Tod::Shift.new(Tod::TimeOfDay.new(c[0]), Tod::TimeOfDay.new(c[1])) + shift2 = Tod::Shift.new(Tod::TimeOfDay.new(c[2]), Tod::TimeOfDay.new(c[3])) + refute shift1.overlaps?(shift2), "Failed with args: #{c}" + end end it "is true when shifts touch with inclusive end" do From 9f891d57db906ee0205925a20ce5d179fcb71d19 Mon Sep 17 00:00:00 2001 From: kennyeni Date: Sat, 30 May 2015 11:23:07 -0500 Subject: [PATCH 4/4] Cleaning the code... --- lib/tod/shift.rb | 20 +++++++++----------- test/tod/shift_test.rb | 4 ++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/tod/shift.rb b/lib/tod/shift.rb index 233f172..3903bdb 100644 --- a/lib/tod/shift.rb +++ b/lib/tod/shift.rb @@ -35,20 +35,18 @@ def include?(tod) def overlaps?(other) max_seconds = TimeOfDay::NUM_SECONDS_IN_DAY - # Standard case - a, b = [self, other].map(&:range) - a, b = [a, b].sort_by(&:first) + # Standard case, when Shifts are on the same day + a, b = [self, other].map(&:range).sort_by(&:first) op = a.exclude_end? ? :> : :>= return true if a.last.send(op, b.first) - # Special cases - if (a.last > max_seconds) || (b.last > max_seconds) - a = Range.new(a.first, a.last - max_seconds, a.exclude_end?) if a.last > max_seconds - b = Range.new(b.first, b.last - max_seconds, b.exclude_end?) if b.last > max_seconds - a, b = [a, b].sort_by(&:last) - b.last.send(op, a.last) && a.last.send(op, b.first) - end - + # Special cases, when Shifts span to the next day + return false if (a.last < max_seconds) && (b.last < max_seconds) + + a = Range.new(a.first, a.last - max_seconds, a.exclude_end?) if a.last > max_seconds + b = Range.new(b.first, b.last - max_seconds, b.exclude_end?) if b.last > max_seconds + a, b = [a, b].sort_by(&:last) + b.last.send(op, a.last) && a.last.send(op, b.first) end def contains?(shift) diff --git a/test/tod/shift_test.rb b/test/tod/shift_test.rb index 384959b..433fa93 100644 --- a/test/tod/shift_test.rb +++ b/test/tod/shift_test.rb @@ -44,7 +44,7 @@ shift2 = Tod::Shift.new(Tod::TimeOfDay.new(13), Tod::TimeOfDay.new(15)) assert shift1.overlaps?(shift2) - # Additional Testing for shifts that Range from one day to another + # Additional Testing for Shifts that span from one day to another cases = [ [5, 8, 7, 2], [7, 2, 1, 8], @@ -72,7 +72,7 @@ shift2 = Tod::Shift.new(Tod::TimeOfDay.new(9), Tod::TimeOfDay.new(12)) refute shift1.overlaps?(shift2) - # Additional Testing for shifts that Range from one day to another + # Additional Testing for Shifts that span from one day to another cases = [ [7, 8, 1, 5], [1, 5, 7, 8],