Skip to content

Commit 3e2dd93

Browse files
johnnyshieldsjamis
andauthored
MONGOID-5674 [Monkey Patch Removal] Remove Object#regexp? (#5714)
* Remove Object#regexp? * deprecate rather than remove --------- Co-authored-by: Jamis Buck <[email protected]>
1 parent f12e442 commit 3e2dd93

File tree

6 files changed

+65
-71
lines changed

6 files changed

+65
-71
lines changed

lib/mongoid/criteria/queryable/extensions/object.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ def __expand_complex__
128128
# obj.regexp?
129129
#
130130
# @return [ false ] Always false.
131+
# @deprecated
131132
def regexp?
132133
false
133134
end
135+
Mongoid.deprecate(self, :regexp?)
134136

135137
module ClassMethods
136138

lib/mongoid/criteria/queryable/extensions/regexp.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ module Regexp
1515
# /\A[123]/.regexp?
1616
#
1717
# @return [ true ] Always true.
18+
# @deprecated
1819
def regexp?; true; end
20+
Mongoid.deprecate(self, :regexp?)
1921

2022
module ClassMethods
2123

@@ -43,7 +45,9 @@ module Raw_
4345
# bson_raw_regexp.regexp?
4446
#
4547
# @return [ true ] Always true.
48+
# @deprecated
4649
def regexp?; true; end
50+
Mongoid.deprecate(self, :regexp?)
4751

4852
module ClassMethods
4953

lib/mongoid/criteria/queryable/extensions/string.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module ClassMethods
8282
# @return [ Hash ] The selection.
8383
def __expr_part__(key, value, negating = false)
8484
if negating
85-
{ key => { "$#{value.regexp? ? "not" : "ne"}" => value }}
85+
{ key => { "$#{__regexp?(value) ? "not" : "ne"}" => value }}
8686
else
8787
{ key => value }
8888
end
@@ -99,9 +99,20 @@ def __expr_part__(key, value, negating = false)
9999
# @return [ String ] The value as a string.
100100
def evolve(object)
101101
__evolve__(object) do |obj|
102-
obj.regexp? ? obj : obj.to_s
102+
__regexp?(obj) ? obj : obj.to_s
103103
end
104104
end
105+
106+
private
107+
108+
# Returns whether the object is Regexp-like.
109+
#
110+
# @param [ Object ] object The object to evaluate.
111+
#
112+
# @return [ Boolean ] Whether the object is Regexp-like.
113+
def __regexp?(object)
114+
object.is_a?(Regexp) || object.is_a?(BSON::Regexp::Raw)
115+
end
105116
end
106117
end
107118
end

spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,4 @@
7878
end
7979
end
8080
end
81-
82-
describe "#regexp?" do
83-
84-
let(:regexp) do
85-
BSON::Regexp::Raw.new('^[123]')
86-
end
87-
88-
it "returns true" do
89-
expect(regexp).to be_regexp
90-
end
91-
end
9281
end

spec/mongoid/criteria/queryable/extensions/regexp_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,4 @@
7979
end
8080
end
8181
end
82-
83-
describe "#regexp?" do
84-
85-
let(:regexp) do
86-
/\A[123]/
87-
end
88-
89-
it "returns true" do
90-
expect(regexp).to be_regexp
91-
end
92-
end
9382
end

spec/mongoid/criteria/queryable/extensions/string_spec.rb

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -181,84 +181,83 @@
181181
end
182182
end
183183

184-
describe "#__expr_part__" do
184+
describe '#__expr_part__' do
185+
subject(:specified) { 'field'.__expr_part__(value) }
186+
let(:value) { 10 }
185187

186-
let(:specified) do
187-
"field".__expr_part__(10)
188+
it 'returns the expression with the value' do
189+
expect(specified).to eq({ 'field' => 10 })
188190
end
189191

190-
it "returns the string with the value" do
191-
expect(specified).to eq({ "field" => 10 })
192-
end
193-
194-
context "with a regexp" do
192+
context 'with a Regexp' do
193+
let(:value) { /test/ }
195194

196-
let(:specified) do
197-
"field".__expr_part__(/test/)
195+
it 'returns the expression with the value' do
196+
expect(specified).to eq({ 'field' => /test/ })
198197
end
198+
end
199199

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

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

206-
context "when negated" do
208+
context 'when negated' do
209+
subject(:specified) { 'field'.__expr_part__(value, true) }
207210

208-
context "with a regexp" do
211+
context 'with a Regexp' do
212+
let(:value) { /test/ }
209213

210-
let(:specified) do
211-
"field".__expr_part__(/test/, true)
214+
it 'returns the expression with the value negated' do
215+
expect(specified).to eq({ 'field' => { '$not' => /test/ } })
212216
end
213-
214-
it "returns the string with the value negated" do
215-
expect(specified).to eq({ "field" => { "$not" => /test/ } })
216-
end
217-
218217
end
219218

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

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

226-
it "returns the string with the value negated" do
227-
expect(specified).to eq({ "field" => { "$ne" => "test" }})
227+
context 'with anything else' do
228+
let(:value) { 'test' }
229+
230+
it 'returns the expression with the value negated' do
231+
expect(specified).to eq({ 'field' => { '$ne' => 'test' }})
228232
end
229233
end
230234
end
231235
end
232236

233-
describe ".evolve" do
237+
describe '.evolve' do
238+
subject(:evolved) { described_class.evolve(object) }
234239

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

237-
let(:regex) do
238-
/\A[123]/.freeze
239-
end
240-
241-
let(:evolved) do
242-
described_class.evolve(regex)
243-
end
244-
245-
it "returns the regex" do
246-
expect(evolved).to eq(regex)
243+
it 'returns the regexp' do
244+
expect(evolved).to eq(/\A[123]/)
247245
end
248246
end
249247

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

252-
let(:object) do
253-
1234
251+
it 'returns the BSON::Regexp::Raw' do
252+
expect(evolved).to eq(BSON::Regexp::Raw.new('^[123]'))
254253
end
254+
end
255255

256-
let(:evolved) do
257-
described_class.evolve(object)
258-
end
256+
context 'when provided an object' do
257+
let(:object) { 1234 }
259258

260-
it "returns the object as a string" do
261-
expect(evolved).to eq("1234")
259+
it 'returns the object as a string' do
260+
expect(evolved).to eq('1234')
262261
end
263262
end
264263
end

0 commit comments

Comments
 (0)