afair / postgresql_cursor

ActiveRecord PostgreSQL Adapter extension for using a cursor to return a large result set
MIT License
599 stars 48 forks source link

Return enumerators when no block given #45

Closed nepalez closed 4 years ago

nepalez commented 5 years ago

Won't it be more natural for the each_instance method (as well as other iterators) w/o a block to return a enumerator instead of the "raw" cursor?

Now I can do this explicitly:

MyModel.all.each_instance.to_enum.with_object([]) { |record, obj| obj << record }

I think this to_enum can be applied to cursor under the hood, namely here: https://github.com/afair/postgresql_cursor/blob/master/lib/postgresql_cursor/active_record/sql_cursor.rb#L34

This would make API a bit more natural IMHO:

MyModel.all.each_instance.with_object([]) { |raw, obj| ... }
afair commented 4 years ago

Thanks for the idea. I hadn't realized the subtle difference between Enumerable and Enumerator. 😄

It seems like a great idea, but removes the ability to process procedurally. The cursor returned can be called with cursor.fetch to return the next row, which is useful for doing merges, or processing involving different streams of data.

Unfortunately, I'll have to back out this change. I don't want to break this feature, as I use it 😅