Module: Mongoid::Criteria::Scrollable

Includes:
Cursors, Fields
Included in:
Mongoid::Criteria
Defined in:
lib/mongoid/criteria/scrollable.rb,
lib/mongoid/criteria/scrollable/fields.rb,
lib/mongoid/criteria/scrollable/cursors.rb,
lib/mongoid/criteria/scrollable/iterator.rb

Defined Under Namespace

Modules: Cursors, Fields Classes: Iterator

Instance Method Summary collapse

Instance Method Details

#scroll(cursor_or_type = nil, &_block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mongoid/criteria/scrollable.rb', line 7

def scroll(cursor_or_type = nil, &_block)
  cursor, cursor_type = cursor_and_type(cursor_or_type)
  raise_multiple_sort_fields_error if multiple_sort_fields?
  criteria = dup
  criteria.merge!(default_sort) if no_sort_option?
  cursor_options = build_cursor_options(criteria)
  cursor = new_cursor(cursor_type, cursor, cursor_options) unless cursor.is_a?(cursor_type)
  raise_mismatched_sort_fields_error!(cursor, cursor_options) if different_sort_fields?(cursor, cursor_options)
  records = find_records(criteria, cursor)
  if block_given?
    previous_cursor = nil
    current_cursor = nil
    records.each do |record|
      previous_cursor ||= cursor_from_record(cursor_type, record, cursor_options.merge(type: :previous))
      current_cursor ||= cursor_from_record(cursor_type, record, cursor_options.merge(include_current: true))
      iterator = Mongoid::Criteria::Scrollable::Iterator.new(
        previous_cursor: previous_cursor,
        next_cursor: cursor_from_record(cursor_type, record, cursor_options),
        current_cursor: current_cursor
      )
      yield record, iterator
    end
  else
    records
  end
end