Interation methods #each, #map_each and more are

implemented and working. Also restructured files
again.
This commit is contained in:
2010-07-29 04:25:42 +03:00
parent d6b65dbc01
commit 438cdd5b68
7 changed files with 184 additions and 58 deletions

View File

@@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "TimeExt" do
describe "Time Calculations" do
before(:each) do
@time = Time.local(2010, 8, 28, 15, 57, 17, 78430)

View File

@@ -0,0 +1,63 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Time Iterations" do
before(:each) do
@now = Time.now
end
it "should iterate over time objects with #each" do
times = []
result = @now.each(:hour) { |time| times << time }
times.should have(24).items
times.should == (1..24).map { |i| @now + i.hours }
result.should == @now
end
it "should iterate over time objects with #beginning_of_each" do
times = []
result = @now.beginning_of_each_hour { |time| times << time }
times.should have(24).items
times.should == (1..24).map { |i| @now.beginning_of_hour + i.hours }
result.should == @now
end
it "should iterate over time objects with #map_each" do
result = @now.map_each_hour { |time| time }
result.should have(24).items
result.should == (1..24).map { |i| @now + i.hours }
end
it "should iterate over time objects with #map_beginning_of_each" do
result = @now.map_beginning_of_each(:hour) { |time| time }
result.should have(24).items
result.should == (1..24).map { |i| @now.beginning_of_hour + i.hours }
end
it "should iterate over time objects with #each and #until via method chaining" do
match = (1..6).map { |i| @now + i.hours }
times = []
result = @now.each(:hour).until(@now + 6.hours) { |time| times << time }
times.should == match
result.should == @now
times = []
result = @now.until(@now + 6.hours).each_hour { |time| times << time }
times.should == match
result.should == @now
end
it "should iterate over time objects with #map_each and #until via method chaining" do
match = (1..6).map { |i| @now + i.hours }
@now.map_each_hour.until(@now + 6.hours) { |time| time }.should == match
@now.until(@now + 6.hours).map_each(:hour) { |time| time }.should == match
end
it "should iterate over time objects backwards with #until set in the past" do
match = (1..6).map { |i| @now - i.hours }
@now.map_each_hour.until(@now - 6.hours) { |time| time }.should == match
@now.until(@now - 6.hours).map_each(:hour) { |time| time }.should == match
end
end