Skip to content

Commit 20c6563

Browse files
johnnyshieldsjamis
andauthored
MONGOID-5669 [Monkey Patch Removal] Remove Array#multi_arged? (#5702)
* 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]>
1 parent 66bd54d commit 20c6563

File tree

4 files changed

+22
-76
lines changed

4 files changed

+22
-76
lines changed

lib/mongoid/criteria/findable.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ module Findable
1414
# criteria.execute_or_raise(id)
1515
#
1616
# @param [ Object ] ids The arguments passed.
17-
# @param [ true | false ] multi Whether there arguments were a list.
17+
# @param [ true | false ] multi Whether there arguments were a list,
18+
# and therefore the return value should be an array.
1819
#
1920
# @raise [ Errors::DocumentNotFound ] If nothing returned.
2021
#
@@ -42,7 +43,7 @@ def execute_or_raise(ids, multi)
4243
def find(*args)
4344
ids = args.__find_args__
4445
raise_invalid if ids.any?(&:nil?)
45-
for_ids(ids).execute_or_raise(ids, args.multi_arged?)
46+
for_ids(ids).execute_or_raise(ids, multi_args?(args))
4647
end
4748

4849
# Adds a criterion to the +Criteria+ that specifies an id that must be matched.
@@ -108,7 +109,7 @@ def from_database(ids)
108109
from_database_selector(ids).entries
109110
end
110111

111-
private def from_database_selector(ids)
112+
def from_database_selector(ids)
112113
if ids.size > 1
113114
any_in(_id: ids)
114115
else
@@ -133,6 +134,20 @@ def mongoize_ids(ids)
133134
end
134135
end
135136

137+
# Indicates whether the given arguments array is a list of values.
138+
# Used by the +find+ method to determine whether to return an array
139+
# or single value.
140+
#
141+
# @example Are these arguments a list of values?
142+
# multi_args?([ 1, 2, 3 ]) #=> true
143+
#
144+
# @param [ Array ] args The arguments.
145+
#
146+
# @return [ true | false ] Whether the arguments are a list.
147+
def multi_args?(args)
148+
args.size > 1 || !args.first.is_a?(Hash) && args.first.resizable?
149+
end
150+
136151
# Convenience method of raising an invalid options error.
137152
#
138153
# @example Raise the error.

lib/mongoid/extensions/array.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
module Mongoid
55
module Extensions
6-
76
# Adds type-casting behavior to Array class.
87
module Array
98

@@ -80,9 +79,11 @@ def blank_criteria?
8079
# [ 1, 2, 3 ].multi_arged?
8180
#
8281
# @return [ true | false ] If the array is multi args.
82+
# @deprecated
8383
def multi_arged?
8484
!first.is_a?(Hash) && first.resizable? || size > 1
8585
end
86+
Mongoid.deprecate(self, :multi_arged?)
8687

8788
# Turn the object from the ruby type we deal with to a Mongo friendly
8889
# type.

lib/mongoid/extensions/object.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ def mongoize
140140
# object.multi_arged?
141141
#
142142
# @return [ false ] false.
143+
# @deprecated
143144
def multi_arged?
144145
false
145146
end
147+
Mongoid.deprecate(self, :multi_arged?)
146148

147149
# Is the object a number?
148150
#

spec/mongoid/extensions/array_spec.rb

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -533,78 +533,6 @@
533533
end
534534
end
535535

536-
describe "#multi_arged?" do
537-
538-
context "when there are multiple elements" do
539-
540-
let(:array) do
541-
[ 1, 2, 3 ]
542-
end
543-
544-
it "returns true" do
545-
expect(array).to be_multi_arged
546-
end
547-
end
548-
549-
context "when there is one element" do
550-
551-
context "when the element is a non enumerable" do
552-
553-
let(:array) do
554-
[ 1 ]
555-
end
556-
557-
it "returns false" do
558-
expect(array).to_not be_multi_arged
559-
end
560-
end
561-
562-
context "when the element is resizable Hash instance" do
563-
564-
let(:array) do
565-
[{'key' => 'value'}]
566-
end
567-
568-
it "returns false" do
569-
expect(array).to_not be_multi_arged
570-
end
571-
end
572-
573-
context "when the element is array of resizable Hash instances" do
574-
575-
let(:array) do
576-
[[{'key1' => 'value2'},{'key1' => 'value2'}]]
577-
end
578-
579-
it "returns true" do
580-
expect(array).to be_multi_arged
581-
end
582-
end
583-
584-
context "when the element is an array" do
585-
586-
let(:array) do
587-
[[ 1 ]]
588-
end
589-
590-
it "returns true" do
591-
expect(array).to be_multi_arged
592-
end
593-
end
594-
595-
context "when the element is a range" do
596-
597-
let(:array) do
598-
[ 1..2 ]
599-
end
600-
601-
it "returns true" do
602-
expect(array).to be_multi_arged
603-
end
604-
end
605-
end
606-
end
607-
608536
describe ".resizable?" do
609537

610538
it "returns true" do

0 commit comments

Comments
 (0)