mirror of
https://github.com/jimeh/time_ext.git
synced 2026-02-19 13:26:39 +00:00
Added #until's sister method, #from.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user