diff --git a/lib/mongoid/criteria/findable.rb b/lib/mongoid/criteria/findable.rb index 09d557b44c..2f116dc353 100644 --- a/lib/mongoid/criteria/findable.rb +++ b/lib/mongoid/criteria/findable.rb @@ -14,7 +14,8 @@ module Findable # criteria.execute_or_raise(id) # # @param [ Object ] ids The arguments passed. - # @param [ true | false ] multi Whether there arguments were a list. + # @param [ true | false ] multi Whether there arguments were a list, + # and therefore the return value should be an array. # # @raise [ Errors::DocumentNotFound ] If nothing returned. # @@ -42,7 +43,7 @@ def execute_or_raise(ids, multi) def find(*args) ids = args.__find_args__ raise_invalid if ids.any?(&:nil?) - for_ids(ids).execute_or_raise(ids, args.multi_arged?) + for_ids(ids).execute_or_raise(ids, multi_args?(args)) end # Adds a criterion to the +Criteria+ that specifies an id that must be matched. @@ -108,7 +109,7 @@ def from_database(ids) from_database_selector(ids).entries end - private def from_database_selector(ids) + def from_database_selector(ids) if ids.size > 1 any_in(_id: ids) else @@ -133,6 +134,20 @@ def mongoize_ids(ids) end end + # Indicates whether the given arguments array is a list of values. + # Used by the +find+ method to determine whether to return an array + # or single value. + # + # @example Are these arguments a list of values? + # multi_args?([ 1, 2, 3 ]) #=> true + # + # @param [ Array ] args The arguments. + # + # @return [ true | false ] Whether the arguments are a list. + def multi_args?(args) + args.size > 1 || !args.first.is_a?(Hash) && args.first.resizable? + end + # Convenience method of raising an invalid options error. # # @example Raise the error. diff --git a/lib/mongoid/extensions/array.rb b/lib/mongoid/extensions/array.rb index 812ac620da..7cf774c754 100644 --- a/lib/mongoid/extensions/array.rb +++ b/lib/mongoid/extensions/array.rb @@ -3,7 +3,6 @@ module Mongoid module Extensions - # Adds type-casting behavior to Array class. module Array @@ -80,9 +79,11 @@ def blank_criteria? # [ 1, 2, 3 ].multi_arged? # # @return [ true | false ] If the array is multi args. + # @deprecated def multi_arged? !first.is_a?(Hash) && first.resizable? || size > 1 end + Mongoid.deprecate(self, :multi_arged?) # Turn the object from the ruby type we deal with to a Mongo friendly # type. diff --git a/lib/mongoid/extensions/object.rb b/lib/mongoid/extensions/object.rb index ec0cd89f91..a63df40c13 100644 --- a/lib/mongoid/extensions/object.rb +++ b/lib/mongoid/extensions/object.rb @@ -136,9 +136,11 @@ def mongoize # object.multi_arged? # # @return [ false ] false. + # @deprecated def multi_arged? false end + Mongoid.deprecate(self, :multi_arged?) # Is the object a number? # diff --git a/spec/mongoid/extensions/array_spec.rb b/spec/mongoid/extensions/array_spec.rb index 18ab1ee2f2..49c402986e 100644 --- a/spec/mongoid/extensions/array_spec.rb +++ b/spec/mongoid/extensions/array_spec.rb @@ -533,78 +533,6 @@ end end - describe "#multi_arged?" do - - context "when there are multiple elements" do - - let(:array) do - [ 1, 2, 3 ] - end - - it "returns true" do - expect(array).to be_multi_arged - end - end - - context "when there is one element" do - - context "when the element is a non enumerable" do - - let(:array) do - [ 1 ] - end - - it "returns false" do - expect(array).to_not be_multi_arged - end - end - - context "when the element is resizable Hash instance" do - - let(:array) do - [{'key' => 'value'}] - end - - it "returns false" do - expect(array).to_not be_multi_arged - end - end - - context "when the element is array of resizable Hash instances" do - - let(:array) do - [[{'key1' => 'value2'},{'key1' => 'value2'}]] - end - - it "returns true" do - expect(array).to be_multi_arged - end - end - - context "when the element is an array" do - - let(:array) do - [[ 1 ]] - end - - it "returns true" do - expect(array).to be_multi_arged - end - end - - context "when the element is a range" do - - let(:array) do - [ 1..2 ] - end - - it "returns true" do - expect(array).to be_multi_arged - end - end - end - end - describe ".resizable?" do it "returns true" do