Permalink
Browse files

Correct usage of Enumerators

  • Loading branch information...
1 parent b942708 commit c687eabd08a5137d9d8cc8922d14df6781e5bab0 Markus Schirp committed Mar 13, 2012
Showing with 36 additions and 10 deletions.
  1. +8 −4 lib/mapper/mapper/mongo.rb
  2. +28 −6 spec/integration/mongo_spec.rb
View
@@ -38,16 +38,20 @@ def read_dumps(query_or_cursor)
end
Enumerator.new do |yielder|
- dump = ::Mapper.symbolize_keys(cursor.next)
- yielder.yield(dump)
+ cursor.each do |dump|
+ dump = ::Mapper.symbolize_keys(dump)
+ yielder.yield(dump)
+ end
end
end
def read_objects(query_or_cursor)
dumps = read_dumps(query_or_cursor)
Enumerator.new do |yielder|
- resource = load(dumps.next)
- yielder.yield(resource)
+ dumps.each do |dump|
+ resource = load(dump)
+ yielder.yield(resource)
+ end
end
end
@@ -28,6 +28,13 @@ def ==(other)
)
end
+ let(:other_person) do
+ Person.new(
+ :firstname => 'John',
+ :lastname => 'Doe'
+ )
+ end
+
let(:person_mapper) do
Mapper::Mapper::Virtus.new(
Person,
@@ -104,38 +111,53 @@ def ==(other)
specify 'allows to read dumps via query' do
mapper.insert_object(person)
+ mapper.insert_object(other_person)
- enumerator = mapper.read_dumps({})
-
- enumerator.to_a.should == [mapper.dump(person)]
+ dumps = mapper.read_dumps({}).to_a
+ dumps.should == [
+ mapper.dump(person),
+ mapper.dump(other_person)
+ ]
end
specify 'allows to read dumps via cursor' do
mapper.insert_object(person)
+ mapper.insert_object(other_person)
cursor = people_collection.find({})
enumerator = mapper.read_dumps(cursor)
- enumerator.to_a.should == [mapper.dump(person)]
+ enumerator.to_a.should == [
+ mapper.dump(person),
+ mapper.dump(other_person)
+ ]
end
specify 'allows to read objects via query' do
mapper.insert_object(person)
+ mapper.insert_object(other_person)
enumerator = mapper.read_objects({})
- enumerator.to_a.should == [person]
+ enumerator.to_a.should == [
+ person,
+ other_person
+ ]
end
specify 'allows to read objects via cursor' do
mapper.insert_object(person)
+ mapper.insert_object(other_person)
cursor = people_collection.find({})
enumerator = mapper.read_objects(cursor)
- enumerator.to_a.should == [person]
+ enumerator.to_a.should == [
+ person,
+ other_person
+ ]
end
specify 'allows to update via object' do

0 comments on commit c687eab

Please sign in to comment.