Sha256: 833394474ee502d0a2496b89bf57e60e8ecc6ed87ef11c48d9104b4fc52ea96d

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 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
      fail 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

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-scroll-0.3.4 lib/mongo/scrollable.rb