Skip to content

MONGOID-5098 Range.mongoize should support end-less and begin-less ranges #5026

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

Merged
merged 8 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/mongoid/extensions/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ module ClassMethods
#
# @param [ Hash ] object The object to demongoize.
#
# @return [ Range ] The range.
# @return [ Range | nil ] The range, or nil if object cannot be represented as range.
#
# @note Ruby 2.6 and lower do not support endless ranges that Ruby 2.7+ support.
def demongoize(object)
object.nil? ? nil : ::Range.new(object["min"], object["max"], object["exclude_end"])
rescue ArgumentError # can be removed when Ruby version >= 2.7
nil
end

# Turn the object from the ruby type we deal with to a Mongo friendly
Expand Down Expand Up @@ -79,8 +83,8 @@ def __mongoize_hash__(object)

def __mongoize_range__(object)
hash = {}
hash['min'] = object.first&.mongoize
hash['max'] = object.last&.mongoize
hash['min'] = object.begin.mongoize if object.begin
hash['max'] = object.end.mongoize if object.end
if object.respond_to?(:exclude_end?) && object.exclude_end?
hash['exclude_end'] = true
end
Expand Down
48 changes: 48 additions & 0 deletions spec/integration/persistence/range_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@
it { expect(subject).to eq(1...3) }
end

context 'when endless' do
ruby_version_gte '2.6'
let(:value) { eval('3..') }
it { expect(subject).to eq(eval('3..')) }
end

context 'when endless exclude_end' do
ruby_version_gte '2.6'
let(:value) { eval('3...') }
it { expect(subject).to eq(eval('3...')) }
end

context 'when beginning-less' do
ruby_version_gte '2.7'
let(:value) { eval('..3') }
it { expect(subject).to eq(eval('..3')) }
end

context 'when beginning-less exclude_end' do
ruby_version_gte '2.7'
let(:value) { eval('...3') }
it { expect(subject).to eq(eval('...3')) }
end

context 'when Hash<String, Integer>' do
let(:value) { { 'min' => 1, 'max' => 3 } }
it { expect(subject).to eq(1..3) }
Expand Down Expand Up @@ -175,6 +199,30 @@
it { expect(subject).to eq('max' => 1, 'min' => 3, 'exclude_end' => true) }
end

context 'when endless' do
ruby_version_gte '2.6'
let(:value) { eval('3..') }
it { expect(subject).to eq('min' => 3) }
end

context 'when endless exclude_end' do
ruby_version_gte '2.6'
let(:value) { eval('3...') }
it { expect(subject).to eq('min' => 3, 'exclude_end' => true) }
end

context 'when beginning-less' do
ruby_version_gte '2.7'
let(:value) { eval('..3') }
it { expect(subject).to eq('max' => 3) }
end

context 'when beginning-less exclude_end' do
ruby_version_gte '2.7'
let(:value) { eval('...3') }
it { expect(subject).to eq('max' => 3, 'exclude_end' => true) }
end

context 'when Hash<String, Integer>' do
let(:value) { { 'min' => 1, 'max' => 3 } }
it { expect(subject).to eq('max' => 3, 'min' => 1) }
Expand Down
124 changes: 122 additions & 2 deletions spec/mongoid/extensions/range_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,86 @@
is_expected.to eq("a"..."z")
end
end

context "when the range is endless" do
let(:hash) { { "min" => 1, "max" => nil } }

context 'kernel can support endless range' do
ruby_version_gte '2.6'

it "returns an alphabetic range" do
is_expected.to eq(eval('1..'))
end
end

context 'kernel cannot support endless range' do
ruby_version_lt '2.6'

it "returns nil" do
is_expected.to be nil
end
end
end

context "when the range is endless with exclude end" do
let(:hash) { { "min" => 1, "max" => nil, "exclude_end" => true } }

context 'kernel can support endless range' do
ruby_version_gte '2.6'

it "returns an alphabetic range" do
is_expected.to eq(eval('1...'))
end
end

context 'kernel cannot support endless range' do
ruby_version_lt '2.6'

it "returns nil" do
is_expected.to be nil
end
end
end

context "when the range is beginning-less" do
let(:hash) { { "min" => nil, "max" => 3 } }

context 'kernel can support beginning-less range' do
ruby_version_gte '2.7'

it "returns an alphabetic range" do
is_expected.to eq(nil..3)
end
end

context 'kernel cannot support beginning-less range' do
ruby_version_lt '2.7'

it "returns nil" do
is_expected.to be nil
end
end
end

context "when the range is beginning-less with exclude end" do
let(:hash) { { "min" => nil, "max" => 3, "exclude_end" => true } }

context 'kernel can support endless range' do
ruby_version_gte '2.7'

it "returns an alphabetic beginning-less" do
is_expected.to eq(eval('...3'))
end
end

context 'kernel cannot support beginning-less range' do
ruby_version_lt '2.7'

it "returns nil" do
is_expected.to be nil
end
end
end
end

shared_examples_for 'mongoize range' do
Expand Down Expand Up @@ -101,6 +181,46 @@
end
end

context 'given an endless range' do
ruby_version_gte '2.6'

let(:range) { eval('5..') }

it "returns the object hash" do
is_expected.to eq("min" => 5)
end
end

context 'given an endless range not inclusive' do
ruby_version_gte '2.6'

let(:range) { eval('5...') }

it "returns the object hash" do
is_expected.to eq("min" => 5, "exclude_end" => true)
end
end

context 'given a beginning-less range' do
ruby_version_gte '2.7'

let(:range) { eval('..5') }

it "returns the object hash" do
is_expected.to eq("max" => 5)
end
end

context 'given an endless range not inclusive' do
ruby_version_gte '2.7'

let(:range) { eval('...5') }

it "returns the object hash" do
is_expected.to eq("max" => 5, "exclude_end" => true)
end
end

context 'given a letter range' do
let(:range) { 'a'..'z' }

Expand Down Expand Up @@ -138,10 +258,10 @@
end

context 'given a Date range' do
let(:range) { Time.at(0).to_date..Time.at(1).to_date }
let(:range) { Date.new(2020, 1, 1)..Date.new(2020, 1, 2) }

it "returns the object hash" do
is_expected.to eq("min" => Time.at(0).in_time_zone, "max" => Time.at(0).in_time_zone)
is_expected.to eq("min" => Time.utc(2020, 1, 1), "max" => Time.utc(2020, 1, 2))
expect(subject["min"].utc?).to be(true)
expect(subject["max"].utc?).to be(true)
end
Expand Down