Added #until's sister method, #from.

This commit is contained in:
2010-07-29 14:26:31 +03:00
parent 3e6b637f75
commit c7e46c25d1
2 changed files with 18 additions and 1 deletions

View File

@@ -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)

View File

@@ -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