-
Notifications
You must be signed in to change notification settings - Fork 12
Gives the previous cursor in the scroll block #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
01eef8d
772010b
b582458
76287e8
3ca3244
089a518
e50e73d
9787664
3e9475a
0d7637f
a1766fb
0a76df8
9c20b52
aa019aa
7f13aca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module Mongoid | ||
module Scroll | ||
class BaseCursor | ||
attr_accessor :value, :tiebreak_id, :field_type, :field_name, :direction, :include_current | ||
attr_accessor :value, :tiebreak_id, :field_type, :field_name, :direction, :include_current, :previous | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about the naming of the previous flag. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
def initialize(value, options = {}) | ||
@value = value | ||
|
@@ -10,6 +10,7 @@ def initialize(value, options = {}) | |
@field_name = options[:field_name] | ||
@direction = options[:direction] || 1 | ||
@include_current = options[:include_current] || false | ||
@previous = options[:previous] || false | ||
end | ||
|
||
def criteria | ||
|
@@ -86,20 +87,23 @@ def extract_field_options(options) | |
field_type: field_type.to_s, | ||
field_name: field_name.to_s, | ||
direction: options[:direction] || 1, | ||
include_current: options[:include_current] || false | ||
include_current: options[:include_current] || false, | ||
previous: options[:previous] || false | ||
} | ||
elsif options && (field = options[:field]) | ||
{ | ||
field_type: field.type.to_s, | ||
field_name: field.name.to_s, | ||
direction: options[:direction] || 1, | ||
include_current: options[:include_current] || false | ||
include_current: options[:include_current] || false, | ||
previous: options[:previous] || false | ||
} | ||
end | ||
end | ||
|
||
def compare_direction | ||
direction == 1 ? '$gt' : '$lt' | ||
dir = previous ? -direction : direction | ||
dir == 1 ? '$gt' : '$lt' | ||
end | ||
|
||
def tiebreak_compare_direction | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module Mongoid | ||
module Scroll | ||
VERSION = '1.0.2'.freeze | ||
VERSION = '1.1.0'.freeze | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,18 @@ | |
describe Mongoid::Scroll::Base64EncodedCursor do | ||
context 'new' do | ||
context 'an empty cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6bnVsbCwiZmllbGRfdHlwZSI6IlN0cmluZyIsImZpZWxkX25hbWUiOiJhX3N0cmluZyIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOm51bGx9' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6bnVsbCwiZmllbGRfdHlwZSI6IlN0cmluZyIsImZpZWxkX25hbWUiOiJhX3N0cmluZyIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOm51bGwsInByZXZpb3VzIjpmYWxzZX0=' } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tokens changed because previous: false is encoded. |
||
subject do | ||
Mongoid::Scroll::Base64EncodedCursor.new base64_string | ||
end | ||
its(:tiebreak_id) { should be_nil } | ||
its(:value) { should be_nil } | ||
its(:criteria) { should eq({}) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'a string field cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6ImEgc3RyaW5nIiwiZmllbGRfdHlwZSI6IlN0cmluZyIsImZpZWxkX25hbWUiOiJhX3N0cmluZyIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2M2RmODA5NDQzNDE3YzdkMmIxMDIifQ==' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6ImEgc3RyaW5nIiwiZmllbGRfdHlwZSI6IlN0cmluZyIsImZpZWxkX25hbWUiOiJhX3N0cmluZyIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2M2RmODA5NDQzNDE3YzdkMmIxMDIiLCJwcmV2aW91cyI6ZmFsc2V9' } | ||
let(:a_value) { 'a string' } | ||
let(:tiebreak_id) { BSON::ObjectId.from_string('64063df809443417c7d2b102') } | ||
let(:criteria) do | ||
|
@@ -32,10 +33,11 @@ | |
its(:value) { should eq a_value } | ||
its(:tiebreak_id) { should eq tiebreak_id } | ||
its(:criteria) { should eq(criteria) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'an id field cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6IjY0MDY0NTg0MDk0NDM0MjgxZmE3MWFiMiIsImZpZWxkX3R5cGUiOiJCU09OOjpPYmplY3RJZCIsImZpZWxkX25hbWUiOiJpZCIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2NDU4NDA5NDQzNDI4MWZhNzFhYjIifQ==' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6IjY0MDY0NTg0MDk0NDM0MjgxZmE3MWFiMiIsImZpZWxkX3R5cGUiOiJCU09OOjpPYmplY3RJZCIsImZpZWxkX25hbWUiOiJpZCIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2NDU4NDA5NDQzNDI4MWZhNzFhYjIiLCJwcmV2aW91cyI6ZmFsc2V9' } | ||
let(:a_value) { BSON::ObjectId('64064584094434281fa71ab2') } | ||
let(:tiebreak_id) { a_value } | ||
let(:criteria) do | ||
|
@@ -52,10 +54,11 @@ | |
its(:value) { should eq a_value } | ||
its(:tiebreak_id) { should eq tiebreak_id } | ||
its(:criteria) { should eq(criteria) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'an integer field cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTAsImZpZWxkX3R5cGUiOiJJbnRlZ2VyIiwiZmllbGRfbmFtZSI6ImFfaW50ZWdlciIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2M2RmODA5NDQzNDE3YzdkMmIxMDgifQ==' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTAsImZpZWxkX3R5cGUiOiJJbnRlZ2VyIiwiZmllbGRfbmFtZSI6ImFfaW50ZWdlciIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2M2RmODA5NDQzNDE3YzdkMmIxMDgiLCJwcmV2aW91cyI6ZmFsc2V9' } | ||
let(:a_value) { 10 } | ||
let(:tiebreak_id) { BSON::ObjectId('64063df809443417c7d2b108') } | ||
let(:criteria) do | ||
|
@@ -74,10 +77,11 @@ | |
its(:value) { should eq a_value } | ||
its(:tiebreak_id) { should eq tiebreak_id } | ||
its(:criteria) { should eq(criteria) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'a date/time field cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTM4NzU5MDEyMy4wLCJmaWVsZF90eXBlIjoiRGF0ZVRpbWUiLCJmaWVsZF9uYW1lIjoiYV9kYXRldGltZSIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2NDNhNzA5NDQzNDIzOWYyZGJmODYifQ==' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTM4NzU5MDEyMy4wLCJmaWVsZF90eXBlIjoiRGF0ZVRpbWUiLCJmaWVsZF9uYW1lIjoiYV9kYXRldGltZSIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2NDNhNzA5NDQzNDIzOWYyZGJmODYiLCJwcmV2aW91cyI6ZmFsc2V9' } | ||
let(:a_value) { DateTime.new(2013, 12, 21, 1, 42, 3, 'UTC') } | ||
let(:tiebreak_id) { BSON::ObjectId('640643a7094434239f2dbf86') } | ||
let(:criteria) do | ||
|
@@ -94,10 +98,11 @@ | |
its(:value) { should eq a_value } | ||
its(:tiebreak_id) { should eq tiebreak_id } | ||
its(:criteria) { should eq(criteria) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'a date field cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTM4NzU4NDAwMCwiZmllbGRfdHlwZSI6IkRhdGUiLCJmaWVsZF9uYW1lIjoiYV9kYXRlIiwiZGlyZWN0aW9uIjoxLCJpbmNsdWRlX2N1cnJlbnQiOmZhbHNlLCJ0aWVicmVha19pZCI6IjY0MDY0MmM5MDk0NDM0MjEyYzRkNDQyMCJ9' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTM4NzU4NDAwMCwiZmllbGRfdHlwZSI6IkRhdGUiLCJmaWVsZF9uYW1lIjoiYV9kYXRlIiwiZGlyZWN0aW9uIjoxLCJpbmNsdWRlX2N1cnJlbnQiOmZhbHNlLCJ0aWVicmVha19pZCI6IjY0MDY0MmM5MDk0NDM0MjEyYzRkNDQyMCIsInByZXZpb3VzIjpmYWxzZX0=' } | ||
let(:tiebreak_id) { BSON::ObjectId('640642c9094434212c4d4420') } | ||
let(:a_value) { Date.new(2013, 12, 21) } | ||
let(:criteria) do | ||
|
@@ -114,10 +119,11 @@ | |
its(:value) { should eq a_value } | ||
its(:tiebreak_id) { should eq tiebreak_id } | ||
its(:criteria) { should eq(criteria) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'a time field cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTM4NzYwNTcyMy4wLCJmaWVsZF90eXBlIjoiVGltZSIsImZpZWxkX25hbWUiOiJhX3RpbWUiLCJkaXJlY3Rpb24iOjEsImluY2x1ZGVfY3VycmVudCI6ZmFsc2UsInRpZWJyZWFrX2lkIjoiNjQwNjNkNGEwOTQ0MzQxNjZiZDA1M2VkIn0=' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTM4NzYwNTcyMy4wLCJmaWVsZF90eXBlIjoiVGltZSIsImZpZWxkX25hbWUiOiJhX3RpbWUiLCJkaXJlY3Rpb24iOjEsImluY2x1ZGVfY3VycmVudCI6ZmFsc2UsInRpZWJyZWFrX2lkIjoiNjQwNjNkNGEwOTQ0MzQxNjZiZDA1M2VkIiwicHJldmlvdXMiOmZhbHNlfQ==' } | ||
let(:item_id) { BSON::ObjectId('640636f209443407333b46d4') } | ||
let(:a_value) { Time.new(2013, 12, 21, 6, 2, 3, '+00:00').utc } | ||
let(:tiebreak_id) { BSON::ObjectId('64063d4a094434166bd053ed') } | ||
|
@@ -136,6 +142,7 @@ | |
its(:tiebreak_id) { tiebreak_id } | ||
its(:tiebreak_id) { should eq tiebreak_id } | ||
its(:criteria) { should eq(criteria) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'an invalid field cursor' do | ||
|
@@ -229,5 +236,22 @@ | |
end.to raise_error Mongoid::Scroll::Errors::UnsupportedFieldTypeError, /The type of the field 'a_array' is not supported: Array./ | ||
end | ||
end | ||
|
||
it 'encode and decode previous option' do | ||
feed_item = Feed::Item.create! | ||
cursor = Mongoid::Scroll::Base64EncodedCursor.from_record feed_item, field_name: 'id', field_type: BSON::ObjectId, previous: true | ||
expect(Mongoid::Scroll::Base64EncodedCursor.new(cursor.to_s).previous).to be_truthy | ||
end | ||
context 'a cursor with previous set to true' do | ||
let(:field_type) { BSON::ObjectId } | ||
let(:field_name) { 'id' } | ||
let(:feed_item) { Feed::Item.create! } | ||
subject do | ||
Mongoid::Scroll::Base64EncodedCursor.from_record feed_item, field_name: field_name, field_type: field_type, previous: true | ||
end | ||
its(:value) { should eq feed_item._id } | ||
its(:field_type) { should eq field_type.to_s } | ||
its(:previous) { should be_truthy } | ||
end | ||
end | ||
end |
Uh oh!
There was an error while loading. Please reload this page.