Class: Mongoid::Scroll::BaseCursor

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/scroll/base_cursor.rb

Direct Known Subclasses

Base64EncodedCursor, Cursor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, options = {}) ⇒ BaseCursor

Returns a new instance of BaseCursor.



6
7
8
9
10
11
12
13
# File 'lib/mongoid/scroll/base_cursor.rb', line 6

def initialize(value, options = {})
  @value = value
  @tiebreak_id = options[:tiebreak_id]
  @field_type = options[:field_type]
  @field_name = options[:field_name]
  @direction = options[:direction] || 1
  @include_current = options[:include_current] || false
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



4
5
6
# File 'lib/mongoid/scroll/base_cursor.rb', line 4

def direction
  @direction
end

#field_nameObject

Returns the value of attribute field_name.



4
5
6
# File 'lib/mongoid/scroll/base_cursor.rb', line 4

def field_name
  @field_name
end

#field_typeObject

Returns the value of attribute field_type.



4
5
6
# File 'lib/mongoid/scroll/base_cursor.rb', line 4

def field_type
  @field_type
end

#include_currentObject

Returns the value of attribute include_current.



4
5
6
# File 'lib/mongoid/scroll/base_cursor.rb', line 4

def include_current
  @include_current
end

#tiebreak_idObject

Returns the value of attribute tiebreak_id.



4
5
6
# File 'lib/mongoid/scroll/base_cursor.rb', line 4

def tiebreak_id
  @tiebreak_id
end

#valueObject

Returns the value of attribute value.



4
5
6
# File 'lib/mongoid/scroll/base_cursor.rb', line 4

def value
  @value
end

Class Method Details

.from_record(record, options) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/mongoid/scroll/base_cursor.rb', line 41

def from_record(record, options)
  cursor = new(nil, options)
  record_value = record.respond_to?(cursor.field_name) ? record.send(cursor.field_name) : record[cursor.field_name]
  cursor.value = cursor.send(:parse_field_value, cursor.field_type, cursor.field_name, record_value)
  cursor.tiebreak_id = record['_id']
  cursor
end

Instance Method Details

#criteriaObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mongoid/scroll/base_cursor.rb', line 15

def criteria
  mongo_value = value.class.mongoize(value) if value
  cursor_criteria = { field_name => { compare_direction => mongo_value } } if mongo_value
  tiebreak_criteria = { field_name => mongo_value, :_id => { tiebreak_compare_direction => tiebreak_id } } if mongo_value && tiebreak_id
  cursor_selector = if Mongoid::Compatibility::Version.mongoid6_or_newer?
                      Mongoid::Criteria::Queryable::Selector.new
                    else
                      Origin::Selector.new
                    end
  cursor_selector['$or'] = [cursor_criteria, tiebreak_criteria].compact if cursor_criteria || tiebreak_criteria
  cursor_selector.__evolve_object_id__
end

#sort_optionsObject



28
29
30
31
32
33
34
# File 'lib/mongoid/scroll/base_cursor.rb', line 28

def sort_options
  {
    field_type: field_type,
    field_name: field_name,
    direction: direction
  }
end

#to_sObject

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/mongoid/scroll/base_cursor.rb', line 36

def to_s
  raise NotImplementedError.new(:to_s)
end