forked from Baguette/dodb.cr
#reverse_each, #reverse_each_with_index added.
parent
787989b447
commit
863bf369b8
23
spec/test.cr
23
spec/test.cr
|
@ -30,11 +30,12 @@ class Ship
|
||||||
# and can easily be extended.
|
# and can easily be extended.
|
||||||
|
|
||||||
class_getter kisaragi = Ship.new("Kisaragi", "Mutsuki")
|
class_getter kisaragi = Ship.new("Kisaragi", "Mutsuki")
|
||||||
class_getter mutsuki = Ship.new("Mutsuki", "Mutsuki", tags: ["name ship"])
|
class_getter mutsuki = Ship.new("Mutsuki", "Mutsuki", tags: ["name ship"])
|
||||||
|
class_getter yayoi = Ship.new("Yayoi", "Mutsuki")
|
||||||
class_getter destroyers = [
|
class_getter destroyers = [
|
||||||
@@kisaragi,
|
|
||||||
@@mutsuki,
|
@@mutsuki,
|
||||||
Ship.new("Yayoi", "Mutsuki"),
|
@@kisaragi,
|
||||||
|
@@yayoi,
|
||||||
Ship.new("Uzuki", "Mutsuki"),
|
Ship.new("Uzuki", "Mutsuki"),
|
||||||
Ship.new("Satsuki", "Mutsuki"),
|
Ship.new("Satsuki", "Mutsuki"),
|
||||||
|
|
||||||
|
@ -157,6 +158,22 @@ describe "DODB::DataBase" do
|
||||||
|
|
||||||
db1.to_a.size.should eq(2)
|
db1.to_a.size.should eq(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "iterates in normal and reversed order" do
|
||||||
|
db = DODB::SpecDataBase.new
|
||||||
|
|
||||||
|
Ship.all_ships.each do |ship|
|
||||||
|
db << ship
|
||||||
|
end
|
||||||
|
|
||||||
|
db.each_with_index do |item, index|
|
||||||
|
item.should eq Ship.all_ships[index]
|
||||||
|
end
|
||||||
|
|
||||||
|
db.reverse_each_with_index do |item, index|
|
||||||
|
item.should eq Ship.all_ships.reverse[index]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "indices" do
|
describe "indices" do
|
||||||
|
|
22
src/dodb.cr
22
src/dodb.cr
|
@ -206,6 +206,28 @@ class DODB::DataBase(V)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def reverse_each_with_index
|
||||||
|
(last_index..0).each do |key|
|
||||||
|
full_path = file_path key
|
||||||
|
|
||||||
|
next unless File.exists? full_path
|
||||||
|
|
||||||
|
begin
|
||||||
|
# FIXME: Only intercept JSON parsing errors.
|
||||||
|
item = read full_path
|
||||||
|
rescue
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
yield item, key
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def reverse_each
|
||||||
|
reverse_each_with_index do |item, index|
|
||||||
|
yield item
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# CAUTION: Very slow. Try not to use.
|
# CAUTION: Very slow. Try not to use.
|
||||||
def to_a
|
def to_a
|
||||||
|
|
Loading…
Reference in New Issue