Skip to content

MONGOID-5399 / 5400 / 5148: Mongoid::Criteria #map should take a splat of field args + yield args to block #5346

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

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
- MONGOID-5148: Add #map to Contextual::Memory and None
- MONGOID-5399: Contextual #map should take a splat of field args
  • Loading branch information
johnnyshields committed Jun 25, 2022
commit b12fe63a045421b5a1a5133e61233c6184d61b8c
24 changes: 24 additions & 0 deletions lib/mongoid/contextual/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,30 @@ def first(*args)
alias :one :first
alias :find_first :first

# Invoke the block for each element of Contextual. Create a new array
# containing the values returned by the block.
#
# If the field name(s) are passed instead of the block, the result
# will be identical to #pluck.
#
# @example Map by some field.
# context.map(:field1)
#
# @example Map with block.
# context.map(&:field1)
#
# @param [ String | Symbol ] *fields The field name(s).
#
# @return [ Array ] The result of mapping, or the plucked
# field values if no block was given.
def map(*fields, &block)
if block_given?
super(&block)
else
pluck(fields)
end
end

# Create the new in memory context.
#
# @example Create the new context.
Expand Down
13 changes: 7 additions & 6 deletions lib/mongoid/contextual/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,23 +314,24 @@ def geo_near(coordinates)
# Invoke the block for each element of Contextual. Create a new array
# containing the values returned by the block.
#
# If the symbol field name is passed instead of the block, additional
# optimizations would be used.
# If the field name(s) are passed instead of the block, the result
# will be identical to #pluck.
#
# @example Map by some field.
# context.map(:field1)
#
# @example Map with block.
# context.map(&:field1)
#
# @param [ Symbol ] field The field name.
# @param [ String | Symbol ] *fields The field name(s).
#
# @return [ Array ] The result of mapping.
def map(field = nil, &block)
# @return [ Array ] The result of mapping, or the plucked
# field values if no block was given.
def map(*fields, &block)
if block_given?
super(&block)
else
criteria.pluck(field)
pluck(fields)
end
end

Expand Down
15 changes: 15 additions & 0 deletions lib/mongoid/contextual/none.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ def each
# @return [ false ] Always false.
def exists?; false; end

# Map fields in null context.
#
# @example Map by some field.
# context.map(:field1)
#
# @example Map with block.
# context.map(&:field1)
#
# @param [ String | Symbol ] *fields The field name(s).
#
# @return [ Array ] An empty Array.
def map(*_fields, &block)
[]
end

# Pluck the field values in null context.
#
# @example Get the values for null context.
Expand Down