Class: Mongoid::Scroll::Cursor

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil, options = {}) ⇒ Cursor

Returns a new instance of Cursor.



6
7
8
9
10
# File 'lib/mongoid/scroll/cursor.rb', line 6

def initialize(value = nil, options = {})
  @field_type, @field_name = Mongoid::Scroll::Cursor.extract_field_options(options)
  @direction = options[:direction] || 1
  parse(value)
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



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

def direction
  @direction
end

#field_nameObject

Returns the value of attribute field_name.



4
5
6
# File 'lib/mongoid/scroll/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/cursor.rb', line 4

def field_type
  @field_type
end

#tiebreak_idObject

Returns the value of attribute tiebreak_id.



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

def tiebreak_id
  @tiebreak_id
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.extract_field_options(options) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/mongoid/scroll/cursor.rb', line 59

def extract_field_options(options)
  if options && (field_name = options[:field_name]) && (field_type = options[:field_type])
    [field_type.to_s, field_name.to_s]
  elsif options && (field = options[:field])
    [field.type.to_s, field.name.to_s]
  else
    raise ArgumentError.new 'Missing options[:field_name] and/or options[:field_type].'
  end
end

.from_record(record, options) ⇒ Object



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

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

.parse_field_value(field_type, field_name, value) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mongoid/scroll/cursor.rb', line 69

def parse_field_value(field_type, field_name, value)
  case field_type.to_s
  when 'BSON::ObjectId', 'Moped::BSON::ObjectId' then value
  when 'String' then value.to_s
  when 'DateTime' then value.is_a?(DateTime) ? value : Time.at(value.to_i).to_datetime
  when 'Time' then value.is_a?(Time) ? value : Time.at(value.to_i)
  when 'Date' then value.is_a?(Date) ? value : Time.at(value.to_i).utc.to_date
  when 'Float' then value.to_f
  when 'Integer' then value.to_i
  else
    raise Mongoid::Scroll::Errors::UnsupportedFieldTypeError.new(field: field_name, type: field_type)
  end
end

.transform_field_value(field_type, field_name, value) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mongoid/scroll/cursor.rb', line 83

def transform_field_value(field_type, field_name, value)
  case field_type.to_s
  when 'BSON::ObjectId', 'Moped::BSON::ObjectId' then value
  when 'String' then value.to_s
  when 'Date' then Time.utc(value.year, value.month, value.day).to_i
  when 'DateTime', 'Time' then value.utc.to_i
  when 'Float' then value.to_f
  when 'Integer' then value.to_i
  else
    raise Mongoid::Scroll::Errors::UnsupportedFieldTypeError.new(field: field_name, type: field_type)
  end
end

Instance Method Details

#criteriaObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mongoid/scroll/cursor.rb', line 12

def criteria
  mongo_value = value.class.mongoize(value) if value
  compare_direction = direction == 1 ? '$gt' : '$lt'
  cursor_criteria = { field_name => { compare_direction => mongo_value } } if mongo_value
  tiebreak_criteria = { field_name => mongo_value, :_id => { compare_direction => tiebreak_id } } if mongo_value && tiebreak_id
  cursor_selector = if Mongoid::Compatibility::Version.mongoid6? || Mongoid::Compatibility::Version.mongoid7?
                      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

#to_sObject



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

def to_s
  tiebreak_id ? [Mongoid::Scroll::Cursor.transform_field_value(field_type, field_name, value), tiebreak_id].join(':') : nil
end