diff --git a/lib/mongoid/criteria/queryable/extensions/object.rb b/lib/mongoid/criteria/queryable/extensions/object.rb index aaf8890063..32605781d4 100644 --- a/lib/mongoid/criteria/queryable/extensions/object.rb +++ b/lib/mongoid/criteria/queryable/extensions/object.rb @@ -128,9 +128,11 @@ def __expand_complex__ # obj.regexp? # # @return [ false ] Always false. + # @deprecated def regexp? false end + Mongoid.deprecate(self, :regexp?) module ClassMethods diff --git a/lib/mongoid/criteria/queryable/extensions/regexp.rb b/lib/mongoid/criteria/queryable/extensions/regexp.rb index 31cdafb95c..514c564b26 100644 --- a/lib/mongoid/criteria/queryable/extensions/regexp.rb +++ b/lib/mongoid/criteria/queryable/extensions/regexp.rb @@ -15,7 +15,9 @@ module Regexp # /\A[123]/.regexp? # # @return [ true ] Always true. + # @deprecated def regexp?; true; end + Mongoid.deprecate(self, :regexp?) module ClassMethods @@ -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 diff --git a/lib/mongoid/criteria/queryable/extensions/string.rb b/lib/mongoid/criteria/queryable/extensions/string.rb index 4cc5133349..1bbd83d0a5 100644 --- a/lib/mongoid/criteria/queryable/extensions/string.rb +++ b/lib/mongoid/criteria/queryable/extensions/string.rb @@ -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 @@ -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 diff --git a/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb b/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb index 11be3f6705..1f8807bf12 100644 --- a/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +++ b/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb @@ -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 diff --git a/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb b/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb index 644d77beb8..9a0639f0bc 100644 --- a/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +++ b/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb @@ -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 diff --git a/spec/mongoid/criteria/queryable/extensions/string_spec.rb b/spec/mongoid/criteria/queryable/extensions/string_spec.rb index 2eb5a722a2..c99c3a4209 100644 --- a/spec/mongoid/criteria/queryable/extensions/string_spec.rb +++ b/spec/mongoid/criteria/queryable/extensions/string_spec.rb @@ -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