Skip to content

Commit

Permalink
MONGOID-5669 [Monkey Patch Removal] Remove Array#multi_arged? (#5702)
Browse files Browse the repository at this point in the history
* Move Array#multi_arged? to Criteria::Findable.multiple_args?

* Update findable_spec.rb

* Clarify code docs

* Add pending for Set spec

* deprecate __multi_arged?__ instead of removing it

---------

Co-authored-by: Jamis Buck <[email protected]>
  • Loading branch information
johnnyshields and jamis authored Nov 6, 2023
1 parent 66bd54d commit 20c6563
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 76 deletions.
21 changes: 18 additions & 3 deletions lib/mongoid/criteria/findable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion lib/mongoid/extensions/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

module Mongoid
module Extensions

# Adds type-casting behavior to Array class.
module Array

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions lib/mongoid/extensions/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,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?
#
Expand Down
72 changes: 0 additions & 72 deletions spec/mongoid/extensions/array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 20c6563

Please sign in to comment.