Class: Mongoid::Scroll::Cursor
- Inherits:
-
Object
- Object
- Mongoid::Scroll::Cursor
- Defined in:
- lib/mongoid/scroll/cursor.rb
Instance Attribute Summary collapse
-
#direction ⇒ Object
Returns the value of attribute direction.
-
#field_name ⇒ Object
Returns the value of attribute field_name.
-
#field_type ⇒ Object
Returns the value of attribute field_type.
-
#tiebreak_id ⇒ Object
Returns the value of attribute tiebreak_id.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
- .extract_field_options(options) ⇒ Object
- .from_record(record, options) ⇒ Object
- .parse_field_value(field_type, field_name, value) ⇒ Object
- .transform_field_value(field_type, field_name, value) ⇒ Object
Instance Method Summary collapse
- #criteria ⇒ Object
-
#initialize(value = nil, options = {}) ⇒ Cursor
constructor
A new instance of Cursor.
- #to_s ⇒ Object
Constructor Details
Instance Attribute Details
#direction ⇒ Object
Returns the value of attribute direction.
4 5 6 |
# File 'lib/mongoid/scroll/cursor.rb', line 4 def direction @direction end |
#field_name ⇒ Object
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_type ⇒ Object
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_id ⇒ Object
Returns the value of attribute tiebreak_id.
4 5 6 |
# File 'lib/mongoid/scroll/cursor.rb', line 4 def tiebreak_id @tiebreak_id end |
#value ⇒ Object
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 () if && (field_name = [:field_name]) && (field_type = [:field_type]) [field_type.to_s, field_name.to_s] elsif && (field = [: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, ) cursor = Mongoid::Scroll::Cursor.new(nil, ) 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
#criteria ⇒ Object
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_s ⇒ Object
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 |