Sha256: 541158785d8e09b9b63945fb90aaadfb9b885ec4c8252ec78101436b7c37e2c4

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

module Mongoid
  module Criterion
    module Scrollable
      def scroll(cursor = nil, &_block)
        criteria = self
        # we don't support scrolling over a criteria with multiple fields
        if criteria.options[:sort] && criteria.options[:sort].keys.size != 1
          fail Mongoid::Scroll::Errors::MultipleSortFieldsError.new(sort: criteria.options[:sort])
        elsif !criteria.options.key?(:sort) || criteria.options[:sort].empty?
          # introduce a default sort order if there's none
          criteria = criteria.asc(:_id)
        end
        # scroll field and direction
        scroll_field = criteria.options[:sort].keys.first
        scroll_direction = criteria.options[:sort].values.first.to_i
        # scroll cursor from the parameter, with value and tiebreak_id
        field = criteria.klass.fields[scroll_field.to_s]
        cursor_options = { field_type: field.type, field_name: scroll_field, direction: scroll_direction }
        cursor = cursor.is_a?(Mongoid::Scroll::Cursor) ? cursor : Mongoid::Scroll::Cursor.new(cursor, cursor_options)
        # scroll
        if block_given?
          cursor_criteria = criteria.dup
          cursor_criteria.selector = { '$and' => [criteria.selector, cursor.criteria] }
          cursor_criteria.order_by(_id: scroll_direction).each do |record|
            yield record, Mongoid::Scroll::Cursor.from_record(record, cursor_options)
          end
        else
          criteria
        end
      end
    end
  end
end

Mongoid::Criteria.send(:include, Mongoid::Criterion::Scrollable)

Version data entries

1 entries across 1 versions & 1 rubygems

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