Skip to content

Commit 8d8b8ae

Browse files
Allowing a block of code/optional args to be passed to better align with Ruby's any? method
1 parent 0412484 commit 8d8b8ae

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

lib/mongoid/criteria/findable.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,50 @@ def multiple_from_db(ids)
8686
# @example Determine if any documents exist
8787
# criteria.any?
8888
#
89+
# @example Determine if any documents match a block
90+
# criteria.any? { |doc| doc.name == "John" }
91+
#
92+
# @param [ Object... ] *args Args to delegate to the target.
93+
#
94+
# @param [ Proc ] &block Block to delegate to the target.
95+
#
8996
# @return [ true | false ] If any documents exist.
90-
def any?
91-
limit(1).count > 0
97+
def any?(*args, &block)
98+
return entries.any?(*args, &block) if args.any? || block_given?
9299
end
93100

94101
# Return true if only one document exists in the criteria.
95102
#
96103
# @example Determine if only one document exists
97104
# criteria.one?
105+
#
106+
# @example Determine if only one document matches a block
107+
# criteria.one? { |doc| doc.name == "John" }
108+
#
109+
# @param [ Object... ] *args Args to delegate to the target.
110+
#
111+
# @param [ Proc ] &block Block to delegate to the target.
98112
#
99113
# @return [ true | false ] If only one document exists.
100-
def one?
101-
limit(2).count == 1
114+
def one?(*args, &block)
115+
return entries.one?(*args, &block) if args.any? || block_given?
102116
end
103117

104118
# Return true if more than one document exists in the criteria.
105119
#
106120
# @example Determine if many documents exist
107121
# criteria.many?
122+
#
123+
# @example Determine if many documents match a block
124+
# criteria.many? { |doc| doc.name.start_with?("J") }
125+
#
126+
# @param [ Object... ] *args Args to delegate to the target.
127+
#
128+
# @param [ Proc ] &block Block to delegate to the target.
108129
#
109130
# @return [ true | false ] If many documents exist.
110-
def many?
111-
limit(2).count > 1
131+
def many?(*args, &block)
132+
return entries.many?(*args, &block) if args.any? || block_given?
112133
end
113134

114135
private

0 commit comments

Comments
 (0)