Class: Mongoid::Scroll::Base64EncodedCursor

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

Overview

Allows to serializer/deserialize the cursor using RFC 4648

Instance Attribute Summary

Attributes inherited from BaseCursor

#direction, #field_name, #field_type, #include_current, #tiebreak_id, #value

Instance Method Summary collapse

Methods inherited from BaseCursor

#criteria, from_record, #sort_options

Constructor Details

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

Returns a new instance of Base64EncodedCursor.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mongoid/scroll/base64_encoded_cursor.rb', line 8

def initialize(value, options = {})
  options = extract_field_options(options)
  if value
    begin
      parsed = ::JSON.parse(::Base64.strict_decode64(value))
    rescue
      raise Mongoid::Scroll::Errors::InvalidBase64CursorError.new(cursor: value)
    end
    super parse_field_value(parsed['field_type'], parsed['field_name'], parsed['value']), {
      field_type: parsed['field_type'],
      field_name: parsed['field_name'],
      direction: parsed['direction'],
      include_current: parsed['include_current'],
      tiebreak_id: parsed['tiebreak_id'] && !parsed['tiebreak_id'].empty? ? BSON::ObjectId.from_string(parsed['tiebreak_id']) : nil
    }
  else
    super nil, options
  end
end

Instance Method Details

#to_sObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/mongoid/scroll/base64_encoded_cursor.rb', line 28

def to_s
  Base64.strict_encode64({
    value: transform_field_value(field_type, field_name, value),
    field_type: field_type.to_s,
    field_name: field_name,
    direction: direction,
    include_current: include_current,
    tiebreak_id: tiebreak_id && tiebreak_id.to_s
  }.to_json)
end