Skip to content
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

[ON HOLD] MONGOID-5703: [Monkey Patch Removal] Extract symbol query macros into a gem #5747

Closed
Closed
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
3 changes: 3 additions & 0 deletions lib/mongoid.rb
Original file line number Diff line number Diff line change
@@ -30,6 +30,9 @@
require "mongoid/warnings"
require "mongoid/utils"

# @todo Extract this to a separate gem.
require "mongoid_symbol_query_macros/symbol_query_macros"

# If we are using Rails then we will include the Mongoid railtie.
# This configures initializers required to integrate Mongoid with Rails.
if defined?(Rails)
1 change: 0 additions & 1 deletion lib/mongoid/criteria/queryable.rb
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@
require "mongoid/criteria/queryable/expandable"
require "mongoid/criteria/queryable/extensions"
require "mongoid/criteria/queryable/key"
require "mongoid/criteria/queryable/macroable"
require "mongoid/criteria/queryable/mergeable"
require "mongoid/criteria/queryable/smash"
require "mongoid/criteria/queryable/aggregable"
9 changes: 0 additions & 9 deletions lib/mongoid/criteria/queryable/aggregable.rb
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ module Queryable

# Provides a DSL around crafting aggregation framework commands.
module Aggregable
extend Macroable

# @attribute [r] pipeline The aggregation pipeline.
attr_reader :pipeline
@@ -42,14 +41,6 @@ def group(operation)
pipeline.group(operation)
end
end
key :avg, :override, "$avg"
key :max, :override, "$max"
key :min, :override, "$min"
key :sum, :override, "$sum"
key :last, :override, "$last"
key :push, :override, "$push"
key :first, :override, "$first"
key :add_to_set, :override, "$addToSet"

# Add a projection ($project) to the aggregation pipeline.
#
16 changes: 0 additions & 16 deletions lib/mongoid/criteria/queryable/extensions/symbol.rb
Original file line number Diff line number Diff line change
@@ -24,22 +24,6 @@ def __expr_part__(value, negating = false)

module ClassMethods

# Adds a method on symbol as a convenience for the MongoDB operator.
#
# @example Add the $in method.
# Symbol.add_key(:in, "$in")
#
# @param [ Symbol ] name The name of the method.
# @param [ Symbol ] strategy The name of the merge strategy.
# @param [ String ] operator The MongoDB operator.
# @param [ String ] additional The additional MongoDB operator.
def add_key(name, strategy, operator, additional = nil, &block)
define_method(name) do
method = "__#{strategy}__".to_sym
Key.new(self, method, operator, additional, &block)
end
end

# Evolves the symbol into a MongoDB friendly value - in this case
# a symbol.
#
27 changes: 0 additions & 27 deletions lib/mongoid/criteria/queryable/macroable.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/mongoid/criteria/queryable/optional.rb
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ module Queryable
# The optional module includes all behavior that has to do with extra
# options surrounding queries, like skip, limit, sorting, etc.
module Optional
extend Macroable

# @attribute [rw] options The query options.
attr_accessor :options
@@ -25,8 +24,6 @@ def ascending(*fields)
sort_with_list(*fields, 1)
end
alias :asc :ascending
key :asc, :override, 1
key :ascending, :override, 1

# Adds the option for telling MongoDB how many documents to retrieve in
# it's batching.
@@ -53,8 +50,6 @@ def descending(*fields)
sort_with_list(*fields, -1)
end
alias :desc :descending
key :desc, :override, -1
key :descending, :override, -1

# Add an index hint to the query options.
#
38 changes: 0 additions & 38 deletions lib/mongoid/criteria/queryable/selectable.rb
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ module Queryable
# document from the database. The selectable module brings all functionality
# to the selectable that has to do with building MongoDB selectors.
module Selectable
extend Macroable

# Constant for a LineString $geometry.
LINE_STRING = "LineString"
@@ -63,7 +62,6 @@ def all(*criteria)
end.reset_strategies!
end
alias :all_in :all
key :all, :union, "$all"

# Add the $and criterion.
#
@@ -158,7 +156,6 @@ def elem_match(criterion)

and_with_operator(criterion, "$elemMatch")
end
key :elem_match, :override, "$elemMatch"

# Add the $exists selection.
#
@@ -183,9 +180,6 @@ def exists(criterion)
Mongoid::Boolean.evolve(value)
end
end
key :exists, :override, "$exists" do |value|
Mongoid::Boolean.evolve(value)
end

# Add a $geoIntersects or $geoWithin selection. Symbol operators must
# be used as shown in the examples to expand the criteria.
@@ -229,20 +223,6 @@ def geo_spatial(criterion)
__merge__(criterion)
end

key :intersects_line, :override, "$geoIntersects", "$geometry" do |value|
{ "type" => LINE_STRING, "coordinates" => value }
end
key :intersects_point, :override, "$geoIntersects", "$geometry" do |value|
{ "type" => POINT, "coordinates" => value }
end
key :intersects_polygon, :override, "$geoIntersects", "$geometry" do |value|
{ "type" => POLYGON, "coordinates" => value }
end
key :within_polygon, :override, "$geoWithin", "$geometry" do |value|
{ "type" => POLYGON, "coordinates" => value }
end
key :within_box, :override, "$geoWithin", "$box"

# Add the $eq criterion to the selector.
#
# @example Add the $eq criterion.
@@ -261,7 +241,6 @@ def eq(criterion)

and_with_operator(criterion, "$eq")
end
key :eq, :override, "$eq"

# Add the $gt criterion to the selector.
#
@@ -281,7 +260,6 @@ def gt(criterion)

and_with_operator(criterion, "$gt")
end
key :gt, :override, "$gt"

# Add the $gte criterion to the selector.
#
@@ -301,7 +279,6 @@ def gte(criterion)

and_with_operator(criterion, "$gte")
end
key :gte, :override, "$gte"

# Adds the $in selection to the selectable.
#
@@ -337,7 +314,6 @@ def in(condition)
end
end
alias :any_in :in
key :in, :intersect, "$in"

# Add the $lt criterion to the selector.
#
@@ -357,7 +333,6 @@ def lt(criterion)

and_with_operator(criterion, "$lt")
end
key :lt, :override, "$lt"

# Add the $lte criterion to the selector.
#
@@ -377,7 +352,6 @@ def lte(criterion)

and_with_operator(criterion, "$lte")
end
key :lte, :override, "$lte"

# Add a $maxDistance selection to the selectable.
#
@@ -414,7 +388,6 @@ def mod(criterion)

and_with_operator(criterion, "$mod")
end
key :mod, :override, "$mod"

# Adds $ne selection to the selectable.
#
@@ -435,7 +408,6 @@ def ne(criterion)
and_with_operator(criterion, "$ne")
end
alias :excludes :ne
key :ne, :override, "$ne"

# Adds a $near criterion to a geo selection.
#
@@ -455,7 +427,6 @@ def near(criterion)

and_with_operator(criterion, "$near")
end
key :near, :override, "$near"

# Adds a $nearSphere criterion to a geo selection.
#
@@ -475,7 +446,6 @@ def near_sphere(criterion)

and_with_operator(criterion, "$nearSphere")
end
key :near_sphere, :override, "$nearSphere"

# Adds the $nin selection to the selectable.
#
@@ -511,7 +481,6 @@ def nin(condition)
end
end
alias :not_in :nin
key :nin, :intersect, "$nin"

# Adds $nor selection to the selectable.
#
@@ -580,7 +549,6 @@ def not(*criteria)
end
end
end
key :not, :override, "$not"

# Negate the arguments, constraining the query to only those documents
# that do NOT match the arguments.
@@ -724,9 +692,6 @@ def with_size(criterion)
::Integer.evolve(value)
end
end
key :with_size, :override, "$size" do |value|
::Integer.evolve(value)
end

# Adds a $type selection to the selectable.
#
@@ -750,9 +715,6 @@ def with_type(criterion)
::Integer.evolve(value)
end
end
key :with_type, :override, "$type" do |value|
::Integer.evolve(value)
end

# Construct a text search selector.
#
30 changes: 30 additions & 0 deletions lib/mongoid_symbol_query_macros/extensions/symbol.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true
# rubocop:todo all

module Mongoid
module SymbolQueryMacros
module Extensions
# Adds query type-casting behavior to Symbol class.
module Symbol
module ClassMethods
# Adds a method on symbol as a convenience for the MongoDB operator.
#
# @example Add the $in method.
# Symbol.add_key(:in, "$in")
#
# @param [ Symbol ] name The name of the method.
# @param [ Symbol ] strategy The name of the merge strategy.
# @param [ String ] operator The MongoDB operator.
# @param [ String ] additional The additional MongoDB operator.
def add_key(name, strategy, operator, additional = nil, &block)
define_method(name) do
Key.new(self, :"__#{strategy}__", operator, additional, &block)
end
end
end
end
end
end
end

::Symbol.extend(Mongoid::SymbolQueryMacros::Extensions::Symbol::ClassMethods)
114 changes: 114 additions & 0 deletions lib/mongoid_symbol_query_macros/macroable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# frozen_string_literal: true
# rubocop:todo all

module Mongoid
module SymbolQueryMacros

# Adds macro behavior for adding symbol methods.
module Macroable
extend ActiveSupport::Concern

class_methods do
# Adds a method on Symbol for convenience in where queries for the
# provided operators.
#
# @example Add a symbol key.
# key :all, '$all
#
# @param [ Symbol ] name The name of the method.
# @param [ Symbol ] strategy The merge strategy.
# @param [ String ] operator The MongoDB operator.
# @param [ String ] additional The additional MongoDB operator.
def key(name, strategy, operator, additional = nil, &block)
::Symbol.add_key(name, strategy, operator, additional, &block)
end
end

included do
key :eq, :override, '$eq'

key :gt, :override, '$gt'

key :gte, :override, '$gte'

key :in, :intersect, '$in'

key :lt, :override, '$lt'

key :lte, :override, '$lte'

key :mod, :override, '$mod'

key :ne, :override, '$ne'

key :nin, :intersect, '$nin'

key :not, :override, '$not'

key :with_size, :override, '$size' do |value|
::Integer.evolve(value)
end

key :with_type, :override, '$type' do |value|
::Integer.evolve(value)
end

key :asc, :override, 1

key :ascending, :override, 1

key :desc, :override, -1

key :descending, :override, -1

key :all, :union, '$all'

key :elem_match, :override, '$elemMatch'

key :exists, :override, '$exists' do |value|
Mongoid::Boolean.evolve(value)
end

key :avg, :override, '$avg'

key :max, :override, '$max'

key :min, :override, '$min'

key :sum, :override, '$sum'

key :last, :override, '$last'

key :push, :override, '$push'

key :first, :override, '$first'

key :add_to_set, :override, '$addToSet'

key :intersects_line, :override, '$geoIntersects', '$geometry' do |value|
{ 'type' => LINE_STRING, 'coordinates' => value }
end

key :intersects_point, :override, '$geoIntersects', '$geometry' do |value|
{ 'type' => POINT, 'coordinates' => value }
end

key :intersects_polygon, :override, '$geoIntersects', '$geometry' do |value|
{ 'type' => POLYGON, 'coordinates' => value }
end

key :near, :override, '$near'

key :near_sphere, :override, '$nearSphere'

key :within_polygon, :override, '$geoWithin', '$geometry' do |value|
{ 'type' => POLYGON, 'coordinates' => value }
end

key :within_box, :override, '$geoWithin', '$box'
end
end
end
end

::Mongoid::Criteria.include(Mongoid::SymbolQueryMacros::Macroable)
3 changes: 3 additions & 0 deletions lib/mongoid_symbol_query_macros/symbol_query_macros.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

require "mongoid_symbol_query_macros/extensions/symbol"
require "mongoid_symbol_query_macros/macroable"
37 changes: 1 addition & 36 deletions spec/mongoid/monkey_patches_spec.rb
Original file line number Diff line number Diff line change
@@ -111,40 +111,6 @@
],
Symbol => %i[
__expr_part__
add_to_set
all
asc
ascending
avg
desc
descending
elem_match
eq
exists
first
gt
gte
in
intersects_line
intersects_point
intersects_polygon
last
lt
lte
max
min
mod
ne
near
near_sphere
nin
not
push
sum
with_size
with_type
within_box
within_polygon
],
TrueClass => %i[is_a?],
Time => %i[
@@ -169,8 +135,7 @@
],
Float => %i[__numeric__],
Integer => %i[__numeric__],
String => %i[__expr_part__],
Symbol => %i[add_key]
String => %i[__expr_part__]
}.each_value(&:sort!)

def mongoid_method?(method)