Skip to content

Commit

Permalink
Merge pull request #7 from RaVbaker/master
Browse files Browse the repository at this point in the history
Making each_row&each_instance methods more concise
  • Loading branch information
afair committed May 13, 2014
2 parents 8f8705f + a0bfe67 commit 2216bac
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ With PostgreSQL, you can work with cursors as follows:
Model.where("id>0").each_row { |hash| Model.process(hash) }

Model.where("id>0").each_instance { |model| model.process! }
Model.where("id>0").each_instance(buffer_size:100000) { |model| model.process! }
Model.where("id>0").each_instance(block_size:100000) { |model| model.process! }

Model.each_row_by_sql("select * from models") { |hash| Model.process(hash) }

Model.each_instance_by_sql("select * from models") { |model| model.process }

All these methods take an options hash to control things more:

buffer_size:n The number of rows to fetch from the database each time (default 1000)
block_size:n The number of rows to fetch from the database each time (default 1000)
while:value Continue looping as long as the block returns this value
until:value Continue looping until the block returns this value
connection:conn Use this connection instead of the current model connection
Expand Down
4 changes: 2 additions & 2 deletions lib/postgresql_cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class ActiveRecord::Relation
#
# Returns the number of rows yielded to the block
def each_row(options={}, &block)
PostgreSQLCursor.new(to_sql).each(&block)
PostgreSQLCursor.new(to_sql, options).each(&block)
end

# Public: Like each_row, but returns an instantiated model object to the block
Expand All @@ -171,7 +171,7 @@ def each_row(options={}, &block)
def each_instance(options={}, &block)
PostgreSQLCursor.new(to_sql, options).each do |row|
model = instantiate(row)
yield model
block.call model
end
end
end

0 comments on commit 2216bac

Please sign in to comment.