Skip to content

Commit 7f13aca

Browse files
committed
throw an error when type is unsupported
1 parent aa019aa commit 7f13aca

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

lib/config/locales/en.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ en:
2727
message: "Unsupported field type."
2828
summary: "The type of the field '%{field}' is not supported: %{type}."
2929
resolution: "Please open a feature request in https://github.com/mongoid/mongoid-scroll."
30-
30+
unsupported_type:
31+
message: "Unsupported type."
32+
summary: "The type supplied in the cursor is not supported: %{type}."
33+
resolution: "The cursor type can be either ':previous' or ':next'."

lib/mongoid/scroll/base_cursor.rb

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ def initialize(value, options = {})
1111
@direction = options[:direction] || 1
1212
@include_current = options[:include_current] || false
1313
@type = options[:type] || :next
14+
15+
raise Mongoid::Scroll::Errors::UnsupportedTypeError.new(type: @type) if ![:previous, :next].include?(@type)
1416
end
1517

1618
def criteria

lib/mongoid/scroll/errors.rb

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
require 'mongoid/scroll/errors/invalid_base64_cursor_error'
77
require 'mongoid/scroll/errors/no_such_field_error'
88
require 'mongoid/scroll/errors/unsupported_field_type_error'
9+
require 'mongoid/scroll/errors/unsupported_type_error'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Mongoid
2+
module Scroll
3+
module Errors
4+
class UnsupportedTypeError < Mongoid::Scroll::Errors::Base
5+
def initialize(opts = {})
6+
super(compose_message('unsupported_type', opts))
7+
end
8+
end
9+
end
10+
end
11+
end

spec/mongoid/cursor_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@
131131
end.to raise_error ArgumentError
132132
end
133133
end
134+
context 'an invalid type cursor' do
135+
let(:feed_item) { Feed::Item.create!(a_string: 'astring') }
136+
it 'raises Mongoid::Scroll::Errors::UnsupportedTypeError' do
137+
expect do
138+
Mongoid::Scroll::Cursor.new "#{feed_item.a_string}:#{feed_item.id}", field_name: 'a_string', field_type: String, include_current: true, type: :invalid
139+
end.to raise_error Mongoid::Scroll::Errors::UnsupportedTypeError, /The type supplied in the cursor is not supported: invalid./
140+
end
141+
end
134142
context 'a cursor with include_current set to true' do
135143
let(:feed_item) { Feed::Item.create!(a_string: 'astring') }
136144
subject do

0 commit comments

Comments
 (0)