Module: PostgreSQLCursor::ActiveRecord::Relation::CursorIterators
- Defined in:
- lib/postgresql_cursor/active_record/relation/cursor_iterators.rb
Instance Method Summary collapse
-
#each_instance(options = {}, &block) ⇒ Object
Public: Like each_row, but returns an instantiated model object to the block.
-
#each_instance_batch(options = {}, &block) ⇒ Object
Public: Like each_row, but yields an array of instantiated model objects to the block.
-
#each_row(options = {}, &block) ⇒ Object
(also: #each_hash)
Public: Executes the query, returning each row as a hash to the given block.
-
#each_row_batch(options = {}, &block) ⇒ Object
(also: #each_hash_batch)
Public: Executes the query, yielding each batch of up to block_size rows where each row is a hash to the given block.
-
#pluck_instances(*cols) ⇒ Object
(also: #pluck_instance)
Plucks the column names from the instances, and return them in an array.
-
#pluck_rows(*cols) ⇒ Object
(also: #pluck_row)
Plucks the column names from the rows, and return them in an array.
Instance Method Details
#each_instance(options = {}, &block) ⇒ Object
Public: Like each_row, but returns an instantiated model object to the block
Paramaters: same as each_row
Example:
Post.where(user_id:123).each_instance { |post| post.process }
Post.where(user_id:123).each_instance.map { |post| post.process }
Returns the number of rows yielded to the block
41 42 43 44 45 46 |
# File 'lib/postgresql_cursor/active_record/relation/cursor_iterators.rb', line 41 def each_instance(={}, &block) = {:connection => self.connection}.merge() cursor = PostgreSQLCursor::Cursor.new(to_unprepared_sql, ) return cursor.each_instance(self, &block) if block_given? cursor.iterate_type(self) end |
#each_instance_batch(options = {}, &block) ⇒ Object
Public: Like each_row, but yields an array of instantiated model objects to the block
Parameters: same as each_row
Example:
Post.where(user_id:123).each_instance_batch do |batch|
Post.process_batch(batch)
end
Post.where(user_id:123).each_instance_batch.map do |batch|
Post.transform_batch(batch)
end
Returns the number of rows yielded to the block
82 83 84 85 86 87 |
# File 'lib/postgresql_cursor/active_record/relation/cursor_iterators.rb', line 82 def each_instance_batch(={}, &block) = {:connection => self.connection}.merge() cursor = PostgreSQLCursor::Cursor.new(to_unprepared_sql, ) return cursor.each_instance_batch(self, &block) if block_given? cursor.iterate_type(self).iterate_batched end |
#each_row(options = {}, &block) ⇒ Object Also known as: each_hash
Public: Executes the query, returning each row as a hash to the given block.
options - Hash to control
fraction: 0.1..1.0 - The cursor_tuple_fraction (default 1.0)
block_size: 1..n - The number of rows to fetch per db block fetch
while: value - Exits loop when block does not return this value.
until: value - Exits loop when block returns this value.
cursor_name: string - Allows you to name your cursor.
Example:
Post.where(user_id:123).each_row { |hash| Post.process(hash) }
Post.each_row.map {|r| r["id"].to_i }
Returns the number of rows yielded to the block
24 25 26 27 28 29 |
# File 'lib/postgresql_cursor/active_record/relation/cursor_iterators.rb', line 24 def each_row(={}, &block) = {:connection => self.connection}.merge() cursor = PostgreSQLCursor::Cursor.new(to_unprepared_sql, ) return cursor.each_row(&block) if block_given? cursor end |
#each_row_batch(options = {}, &block) ⇒ Object Also known as: each_hash_batch
Public: Executes the query, yielding each batch of up to block_size rows where each row is a hash to the given block.
Parameters: same as each_row
Example:
Post.where(user_id:123).each_row_batch do |batch|
Post.process_batch(batch)
end
Post.each_row_batch.map { |batch| Post.transform_batch(batch) }
Returns the number of rows yielded to the block
60 61 62 63 64 65 |
# File 'lib/postgresql_cursor/active_record/relation/cursor_iterators.rb', line 60 def each_row_batch(={}, &block) = {:connection => self.connection}.merge() cursor = PostgreSQLCursor::Cursor.new(to_unprepared_sql, ) return cursor.each_row_batch(&block) if block_given? cursor.iterate_batched end |
#pluck_instances(*cols) ⇒ Object Also known as: pluck_instance
Plucks the column names from the instances, and return them in an array
98 99 100 101 102 |
# File 'lib/postgresql_cursor/active_record/relation/cursor_iterators.rb', line 98 def pluck_instances(*cols) = cols.last.is_a?(Hash) ? cols.pop : {} [:connection] = self.connection self.each_instance().pluck(*cols) end |
#pluck_rows(*cols) ⇒ Object Also known as: pluck_row
Plucks the column names from the rows, and return them in an array
90 91 92 93 94 |
# File 'lib/postgresql_cursor/active_record/relation/cursor_iterators.rb', line 90 def pluck_rows(*cols) = cols.last.is_a?(Hash) ? cols.pop : {} [:connection] = self.connection self.each_row().pluck(*cols) end |