Added upto method to core_ext/time/calculations so thatyou could step through time with different step sizes.
The sizes are any time methods that works on Fixnum, i.e. :days, :years, :minutes, :seconds, :hours, :months.
This is my first patch and I dont see anywhere to attach the diff file, so i am going to just paste it below.
PATCH:
Index: activesupport/test/core_ext/time_ext_test.rb
===================================================================
--- activesupport/test/core_ext/time_ext_test.rb (revision 6146)
+++ activesupport/test/core_ext/time_ext_test.rb (working copy)
@@ -293,7 +293,87 @@
midnight = Time.local(2005, 2, 21, 0, 0, 0)
assert_equal midnight.midnight, (midnight + 1.hour + 0.000001).midnight
end
+
+
+ def test_upto_by_days
+ time = Time.local(2005, 2, 21, 17, 44, 30)
+ end_date = time + 10.days
+ counter = 0
+ time.upto(end_date) do |date|
+ assert_equal date, (time + counter.days)
+ counter += 1
+ end
+
+ end
+
+ def test_upto_by_months
+ time = Time.local(2005, 2, 21, 17, 44, 30)
+ end_date = time + 10.months
+ counter = 0
+ time.upto(end_date, :month) do |date|
+ assert_equal date, (time + counter.months)
+ counter += 1
+ end
+
+ end
+
+ def test_upto_by_years
+ time = Time.local(2005, 2, 21, 17, 44, 30)
+ end_date = time + 10.years
+ counter = 0
+ time.upto(end_date, :year) do |date|
+ assert_equal date, (time + counter.years)
+ counter += 1
+ end
+
+ end
+
+ def test_upto_by_seconds
+ time = Time.local(2005, 2, 21, 17, 44, 30)
+ end_date = time + 10.seconds
+ counter = 0
+ time.upto(end_date, :seconds) do |date|
+ assert_equal date, (time + counter.seconds)
+ counter += 1
+ end
+
+ end
+
+ def test_upto_by_hours
+ time = Time.local(2005, 2, 21, 17, 44, 30)
+ end_date = time + 10.hours
+ counter = 0
+ time.upto(end_date, :hours) do |date|
+ assert_equal date, (time + counter.hours)
+ counter += 1
+ end
+
+ end
+
+ def test_upto_by_minutes
+ time = Time.local(2005, 2, 21, 17, 44, 30)
+ end_date = time + 10.minutes
+ counter = 0
+ time.upto(end_date, :minutes) do |date|
+ assert_equal date, (time + counter.minutes)
+ counter += 1
+ end
+
+ end
+
+ def test_upto_with_bad_step_size
+ time = Time.local(2005, 2, 21, 17, 44, 30)
+ end_date = time + 10.minutes
+ assert_raise NoMethodError do
+ time.upto(end_date, :non_step_size) do |date|
+ puts date
+ end
+ end
+
+
+ end
+
def test_days_in_month
assert_equal 31, Time.days_in_month(1, 2005)
Index: activesupport/lib/active_support/core_ext/time/calculations.rb
===================================================================
--- activesupport/lib/active_support/core_ext/time/calculations.rb (revision 6146)
+++ activesupport/lib/active_support/core_ext/time/calculations.rb (working copy)
@@ -196,6 +196,23 @@
self.since(1.day)
end
+
+
+ # Convenience method to step though time in different increments
+ # step_sizes = :days, :months, :years, :seconds, :minutes, :hours
+ def upto(end_date, step_size = :days)
+ start_date = self
+ stepper = 1.send(step_size.to_s.pluralize.to_sym)
+
+ while start_date < end_date
+ yield start_date
+ start_date += stepper
+ end
+ rescue NoMethodError => e
+ raise NoMethodError("Invalid Step Size")
+
+ end
+
def plus_with_duration(other) #:nodoc:
if ActiveSupport::Duration === other
other.since(self)