module Mongoid::Criteria::Queryable::Mergeable
def __add__(criterion, operator)
- Since: - 1.0.0
Returns:
-
(Mergeable)- The new mergeable.
Parameters:
-
operator(String) -- The MongoDB operator. -
criterion(Hash) -- The criteria.
Other tags:
- Example: Add the criterion. -
Other tags:
- Api: - private
def __add__(criterion, operator) with_strategy(:__add__, criterion, operator) end
def __expanded__(criterion, outer, inner)
- Since: - 1.0.0
Returns:
-
(Mergeable)- The new mergeable.
Parameters:
-
inner(String) -- The inner MongoDB operator. -
outer(String) -- The outer MongoDB operator. -
criterion(Hash) -- The criteria.
Other tags:
- Example: Add the criterion. -
Other tags:
- Api: - private
def __expanded__(criterion, outer, inner) selection(criterion) do |selector, field, value| selector.store(field, { outer => { inner => value }}) end end
def __intersect__(criterion, operator)
- Since: - 1.0.0
Returns:
-
(Mergeable)- The new mergeable.
Parameters:
-
operator(String) -- The MongoDB operator. -
criterion(Hash) -- The criteria.
Other tags:
- Example: Add the criterion. -
Other tags:
- Api: - private
def __intersect__(criterion, operator) with_strategy(:__intersect__, criterion, operator) end
def __merge__(criterion)
- Since: - 2.0.0
Returns:
-
(Mergeable)- The cloned object.
Parameters:
-
criterion(Hash) -- The criteria.
Other tags:
- Example: Straight merge the expanded criterion. -
Other tags:
- Api: - private
def __merge__(criterion) selection(criterion) do |selector, field, value| selector.merge!(field.__expr_part__(value)) end end
def __multi__(criteria, operator)
- Since: - 1.0.0
Returns:
-
(Mergeable)- The new mergeable.
Parameters:
-
operator(String) -- The MongoDB operator. -
criteria(Array) -- Multiple key/value pair
Other tags:
- Example: Add the criterion. -
Other tags:
- Api: - private
def __multi__(criteria, operator) clone.tap do |query| sel = query.selector criteria.flatten.each do |expr| next unless expr result_criteria = sel[operator] || [] if expr.is_a?(Selectable) expr = expr.selector end normalized = _mongoid_normalize_expr(expr) sel.store(operator, result_criteria.push(normalized)) end end end
def __override__(criterion, operator)
- Since: - 1.0.0
Returns:
-
(Mergeable)- The new mergeable.
Parameters:
-
operator(String) -- The MongoDB operator. -
criterion(Hash | Criteria) -- The criteria.
Other tags:
- Example: Add the criterion. -
Other tags:
- Api: - private
def __override__(criterion, operator) if criterion.is_a?(Selectable) criterion = criterion.selector end selection(criterion) do |selector, field, value| expression = prepare(field, operator, value) existing = selector[field] if existing.respond_to?(:merge!) selector.store(field, existing.merge!(expression)) else selector.store(field, expression) end end end
def __union__(criterion, operator)
- Since: - 1.0.0
Returns:
-
(Mergeable)- The new mergeable.
Parameters:
-
operator(String) -- The MongoDB operator. -
criterion(Hash) -- The criteria.
Other tags:
- Example: Add the criterion. -
Other tags:
- Api: - private
def __union__(criterion, operator) with_strategy(:__union__, criterion, operator) end
def _mongoid_add_top_level_operation(operator, criteria)
- Api: - private
def _mongoid_add_top_level_operation(operator, criteria) ten the criteria. The idea is that predicates in MongoDB always hashes and are never arrays. This method additionally ws Criteria instances as predicates. flattening is existing Mongoid behavior but we could possibly rid of it as applications can splat their predicates, or ten if needed. tap do |query| = query.selector goid_flatten_arrays(criteria).each do |criterion| criterion.is_a?(Selectable) expr = _mongoid_normalize_expr(criterion.selector) se expr = criterion d sel.empty? sel.store(operator, [expr]) sif sel.keys == [operator] sel.store(operator, sel[operator] + [expr]) se operands = [sel.dup] + [expr] sel.clear sel.store(operator, operands) d
def _mongoid_flatten_arrays(array)
evaluates the criteria, which we do not want. Hence this method
Calling .flatten on an array which includes a Criteria instance
def _mongoid_flatten_arrays(array) [] g = array pending.empty? = pending.shift tem.nil? skip f item.is_a?(Array) nding += item t << item
def _mongoid_normalize_expr(expr)
- Api: - private
def _mongoid_normalize_expr(expr) nject({}) do |hash, (field, value)| .merge!(field.__expr_part__(value.__expand_complex__))
def intersect
- Since: - 1.0.0
Returns:
-
(Mergeable)- The intersect flagged mergeable.
Other tags:
- Example: Use intersection on the next call. -
def intersect use(:__intersect__) end
def override
- Since: - 1.0.0
Returns:
-
(Mergeable)- The override flagged mergeable.
Other tags:
- Example: Use override on the next call. -
def override use(:__override__) end
def prepare(field, operator, value)
- Since: - 1.0.0
Returns:
-
(Object)- The serialized value.
Parameters:
-
value(Object) -- The value. -
field(String) -- The name of the field.
Other tags:
- Example: Prepare the value. -
Other tags:
- Api: - private
def prepare(field, operator, value) unless operator =~ /exists|type|size/ value = value.__expand_complex__ field = field.to_s name = aliases[field] || field serializer = serializers[name] value = serializer ? serializer.evolve(value) : value end selection = { operator => value } negating? ? { "$not" => selection } : selection end
def reset_strategies!
- Since: - 1.0.0
Returns:
-
(Criteria)- self.
Other tags:
- Example: Reset the strategies. -
def reset_strategies! self.strategy = nil self.negating = nil self end
def union
- Since: - 1.0.0
Returns:
-
(Mergeable)- The union flagged mergeable.
Other tags:
- Example: Use union on the next call. -
def union use(:__union__) end
def use(strategy)
- Since: - 1.0.0
Returns:
-
(Mergeable)- The existing mergeable.
Parameters:
-
strategy(Symbol) -- The strategy to use.
Other tags:
- Example: Use intersection. -
Other tags:
- Api: - private
def use(strategy) tap do |mergeable| mergeable.strategy = strategy end end
def with_strategy(strategy, criterion, operator)
- Since: - 1.0.0
Returns:
-
(Mergeable)- The cloned query.
Parameters:
-
operator(String) -- The MongoDB operator. -
criterion(Object) -- The criterion to add. -
strategy(Symbol) -- The name of the strategy method.
Other tags:
- Example: Add criterion with a strategy. -
Other tags:
- Api: - private
def with_strategy(strategy, criterion, operator) selection(criterion) do |selector, field, value| selector.store( field, selector[field].send(strategy, prepare(field, operator, value)) ) end end