From c7e46c25d107288c89f1d1e39c81b4a452d6a0f7 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Thu, 29 Jul 2010 14:26:31 +0300 Subject: [PATCH] Added #until's sister method, #from. --- lib/time_ext/core_ext/time/iterations.rb | 13 ++++++++++++- spec/time_iterations_spec.rb | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/time_ext/core_ext/time/iterations.rb b/lib/time_ext/core_ext/time/iterations.rb index 57b9a81..cf9a0ee 100644 --- a/lib/time_ext/core_ext/time/iterations.rb +++ b/lib/time_ext/core_ext/time/iterations.rb @@ -25,7 +25,7 @@ class Time end end - # Used togeter with #each to specify end of interation. + # Used togeter with #each and other iteration methods to specify end of interation. def until(time, &block) time = time.to_time if time.is_a?(::Date) @until = time @@ -34,6 +34,17 @@ class Time end alias :till :until + # Used together with #each and other interation methods to specify start of iteration, and end will be current object. + def from(time, &block) + time = time.to_time if time.is_a?(::Date) + method, args = @method_chain.pop if block_given? + if !method.nil? + time.until(self).send(method, *args, &block) + else + time.until(self) + end + end + # Executes passed block for each "unit" of time specified, with a new time object for each interval passed to the block. def each(unit, options = {}, &block) iterate(unit, options.merge(:map_result => false), &block) diff --git a/spec/time_iterations_spec.rb b/spec/time_iterations_spec.rb index 0aa85f4..ce07002 100644 --- a/spec/time_iterations_spec.rb +++ b/spec/time_iterations_spec.rb @@ -60,4 +60,10 @@ describe "Time Iterations" do @now.until(@now - 6.hours).map_each(:hour) { |time| time }.should == match end + it "should iterate over time objects with #map_each and #from via method chaining" do + match = (1..6).map { |i| @now + i.hours } + (@now + 6.hours).map_each_hour.from(@now) { |time| time }.should == match + (@now + 6.hours).from(@now).map_each(:hour) { |time| time }.should == match + end + end \ No newline at end of file