Skip to content

MONGOID-5669 [Monkey Patch Removal] Remove Array#multi_arged? #5702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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?
#
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