Fixes reversed: true.

Hey crystal, why don’t you have reversed ranges?
This commit is contained in:
Luka Vandervelden 2020-01-13 13:48:24 +01:00
parent 65c607d9fc
commit 6dc3a6d2f7
2 changed files with 20 additions and 9 deletions

View File

@ -166,13 +166,17 @@ describe "DODB::DataBase" do
db << ship db << ship
end end
# The two #each test iteration.
db.each_with_index do |item, index| db.each_with_index do |item, index|
item.should eq Ship.all_ships[index] item.should eq Ship.all_ships[index]
end end
db.each_with_index(reversed: true) do |item, index| db.each_with_index(reversed: true) do |item, index|
item.should eq Ship.all_ships.reverse[index] item.should eq Ship.all_ships[index]
end end
# Actual reversal is tested here.
db.to_a(reversed: true).should eq db.to_a.reverse
end end
it "respects the provided offsets if any" do it "respects the provided offsets if any" do

View File

@ -179,19 +179,26 @@ class DODB::DataBase(V)
end end
private def each_key(reversed = false) private def each_key(reversed = false)
range = if reversed start = 0
(last_index..0) _end = last_index
else step = 1
(0..last_index)
if reversed
start = _end
_end = 0
step = -1
end end
range.each do |key| key = start
while step == 1 ? key <= _end : key >= _end
full_path = file_path key full_path = file_path key
next unless File.exists? full_path if File.exists? full_path
yield key, full_path yield key, full_path
end end
key = key + step
end
end end
## ##