moped
moped copied to clipboard
Add @cache to Moped::Cursor for out-of-block iteration
Caveat: This is potentially very risky, so feel free to reject. I'm not familiar enough with Mongoid to understand all the implications of this change. That said, we've been using it in production for some time without any apparent ill effects.
The Mongo Ruby driver (and thus Mongoid 2.x) supported iterating over a cursor outside a block, using such operations as take(). For example:
batch_size = 10
cursor = session[:users].find.cursor
while (users = cursor.take(batch_size)).present?
# Do stuff...
end
# Do more stuff...
This was a convenient pattern for implementing lots of algorithms in a more readable way than if they were written using each(). This patch reintroduces this convenience.
cc: @dblock
Coverage remained the same when pulling 2d854a0b518459c841d292ec6f3ca03d43e789ef on fancyremarker:cursor-cache-updated into 073e30bbc49f10d71a1ff3c8462f779f457e28f0 on mongoid:master.
Coverage decreased (-0.0%) when pulling 2d854a0b518459c841d292ec6f3ca03d43e789ef on fancyremarker:cursor-cache-updated into 073e30bbc49f10d71a1ff3c8462f779f457e28f0 on mongoid:master.
@fancyremarker looks like Travis build failed, not sure if this is introduced by this change
@dblock: I couldn't see how it was connected, but that's because of my own ignorance. FWIW, specs did pass locally for me.