Skip to content

Commit

Permalink
MONGOID-5674 [Monkey Patch Removal] Remove Object#regexp? (#5714)
Browse files Browse the repository at this point in the history
* Remove Object#regexp?

* deprecate rather than remove

---------

Co-authored-by: Jamis Buck <[email protected]>
  • Loading branch information
johnnyshields and jamis authored Nov 8, 2023
1 parent f12e442 commit 3e2dd93
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 71 deletions.
2 changes: 2 additions & 0 deletions lib/mongoid/criteria/queryable/extensions/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ def __expand_complex__
# obj.regexp?
#
# @return [ false ] Always false.
# @deprecated
def regexp?
false
end
Mongoid.deprecate(self, :regexp?)

module ClassMethods

Expand Down
4 changes: 4 additions & 0 deletions lib/mongoid/criteria/queryable/extensions/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ module Regexp
# /\A[123]/.regexp?
#
# @return [ true ] Always true.
# @deprecated
def regexp?; true; end
Mongoid.deprecate(self, :regexp?)

module ClassMethods

Expand Down Expand Up @@ -43,7 +45,9 @@ module Raw_
# bson_raw_regexp.regexp?
#
# @return [ true ] Always true.
# @deprecated
def regexp?; true; end
Mongoid.deprecate(self, :regexp?)

module ClassMethods

Expand Down
15 changes: 13 additions & 2 deletions lib/mongoid/criteria/queryable/extensions/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module ClassMethods
# @return [ Hash ] The selection.
def __expr_part__(key, value, negating = false)
if negating
{ key => { "$#{value.regexp? ? "not" : "ne"}" => value }}
{ key => { "$#{__regexp?(value) ? "not" : "ne"}" => value }}
else
{ key => value }
end
Expand All @@ -99,9 +99,20 @@ def __expr_part__(key, value, negating = false)
# @return [ String ] The value as a string.
def evolve(object)
__evolve__(object) do |obj|
obj.regexp? ? obj : obj.to_s
__regexp?(obj) ? obj : obj.to_s
end
end

private

# Returns whether the object is Regexp-like.
#
# @param [ Object ] object The object to evaluate.
#
# @return [ Boolean ] Whether the object is Regexp-like.
def __regexp?(object)
object.is_a?(Regexp) || object.is_a?(BSON::Regexp::Raw)
end
end
end
end
Expand Down
11 changes: 0 additions & 11 deletions spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,4 @@
end
end
end

describe "#regexp?" do

let(:regexp) do
BSON::Regexp::Raw.new('^[123]')
end

it "returns true" do
expect(regexp).to be_regexp
end
end
end
11 changes: 0 additions & 11 deletions spec/mongoid/criteria/queryable/extensions/regexp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,4 @@
end
end
end

describe "#regexp?" do

let(:regexp) do
/\A[123]/
end

it "returns true" do
expect(regexp).to be_regexp
end
end
end
93 changes: 46 additions & 47 deletions spec/mongoid/criteria/queryable/extensions/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,84 +181,83 @@
end
end

describe "#__expr_part__" do
describe '#__expr_part__' do
subject(:specified) { 'field'.__expr_part__(value) }
let(:value) { 10 }

let(:specified) do
"field".__expr_part__(10)
it 'returns the expression with the value' do
expect(specified).to eq({ 'field' => 10 })
end

it "returns the string with the value" do
expect(specified).to eq({ "field" => 10 })
end

context "with a regexp" do
context 'with a Regexp' do
let(:value) { /test/ }

let(:specified) do
"field".__expr_part__(/test/)
it 'returns the expression with the value' do
expect(specified).to eq({ 'field' => /test/ })
end
end

it "returns the symbol with the value" do
expect(specified).to eq({ "field" => /test/ })
end
context 'with a BSON::Regexp::Raw' do
let(:value) { BSON::Regexp::Raw.new('^[123]') }

it 'returns the expression with the value' do
expect(specified).to eq({ 'field' => BSON::Regexp::Raw.new('^[123]') })
end
end

context "when negated" do
context 'when negated' do
subject(:specified) { 'field'.__expr_part__(value, true) }

context "with a regexp" do
context 'with a Regexp' do
let(:value) { /test/ }

let(:specified) do
"field".__expr_part__(/test/, true)
it 'returns the expression with the value negated' do
expect(specified).to eq({ 'field' => { '$not' => /test/ } })
end

it "returns the string with the value negated" do
expect(specified).to eq({ "field" => { "$not" => /test/ } })
end

end

context "with anything else" do
context 'with a BSON::Regexp::Raw' do
let(:value) { BSON::Regexp::Raw.new('^[123]') }

let(:specified) do
"field".__expr_part__('test', true)
it 'returns the expression with the value' do
expect(specified).to eq({ 'field' => { '$not' => BSON::Regexp::Raw.new('^[123]') } })
end
end

it "returns the string with the value negated" do
expect(specified).to eq({ "field" => { "$ne" => "test" }})
context 'with anything else' do
let(:value) { 'test' }

it 'returns the expression with the value negated' do
expect(specified).to eq({ 'field' => { '$ne' => 'test' }})
end
end
end
end

describe ".evolve" do
describe '.evolve' do
subject(:evolved) { described_class.evolve(object) }

context "when provided a regex" do
context 'when provided a Regexp' do
let(:object) { /\A[123]/.freeze }

let(:regex) do
/\A[123]/.freeze
end

let(:evolved) do
described_class.evolve(regex)
end

it "returns the regex" do
expect(evolved).to eq(regex)
it 'returns the regexp' do
expect(evolved).to eq(/\A[123]/)
end
end

context "when provided an object" do
context 'when provided a BSON::Regexp::Raw' do
let(:object) { BSON::Regexp::Raw.new('^[123]') }

let(:object) do
1234
it 'returns the BSON::Regexp::Raw' do
expect(evolved).to eq(BSON::Regexp::Raw.new('^[123]'))
end
end

let(:evolved) do
described_class.evolve(object)
end
context 'when provided an object' do
let(:object) { 1234 }

it "returns the object as a string" do
expect(evolved).to eq("1234")
it 'returns the object as a string' do
expect(evolved).to eq('1234')
end
end
end
Expand Down

0 comments on commit 3e2dd93

Please sign in to comment.