Sha256: e85039665a765a66fc741b5577c391a517ea1c0b46c81520c9a968a0c62833c6

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module Mongo
  module Scrollable
    def scroll(cursor = nil, options = nil, &_block)
      view = self
      # we don't support scrolling over a view with multiple fields
      raise Mongoid::Scroll::Errors::MultipleSortFieldsError.new(sort: view.sort) if view.sort && view.sort.keys.size != 1
      # scroll field and direction
      scroll_field = view.sort ? view.sort.keys.first : :_id
      scroll_direction = view.sort ? view.sort.values.first.to_i : 1
      # scroll cursor from the parameter, with value and tiebreak_id
      options = { field_type: BSON::ObjectId } unless options
      cursor_options = { field_name: scroll_field, direction: scroll_direction }.merge(options)
      cursor = cursor.is_a?(Mongoid::Scroll::Cursor) ? cursor : Mongoid::Scroll::Cursor.new(cursor, cursor_options)
      # make a view
      view = Mongo::Collection::View.new(
        view.collection,
        view.selector.merge(cursor.criteria),
        sort: (view.sort || {}).merge(_id: scroll_direction),
        skip: skip,
        limit: limit
      )
      # scroll
      if block_given?
        view.each do |record|
          yield record, Mongoid::Scroll::Cursor.from_record(record, cursor_options)
        end
      else
        view
      end
    end
  end
end

Mongo::Collection::View.send(:include, Mongo::Scrollable)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-scroll-0.3.7 lib/mongo/scrollable.rb
mongoid-scroll-0.3.6 lib/mongo/scrollable.rb
mongoid-scroll-0.3.5 lib/mongo/scrollable.rb