module Mongoid::Criteria::Queryable::Extensions::Hash

Adds query type-casting behavior to Hash class.

Public Instance Methods

__add__(object) click to toggle source

Add an object to a hash using the merge strategies.

@example Add an object to a hash.

{ field: value }.__add__({ field: other_value })

@param [ Hash ] object The other hash to add.

@return [ Hash ] The hash with object added.

# File lib/mongoid/criteria/queryable/extensions/hash.rb, line 20
def __add__(object)
  apply_strategy(:__add__, object)
end
__add_from_array__(array) click to toggle source

Merge this hash into the provided array.

@example Merge the hash into the array.

{ field: value }.__add_from_array__([ 1, 2 ])

@param [ Array ] array The array to add to.

@return [ Hash ] The merged hash.

# File lib/mongoid/criteria/queryable/extensions/hash.rb, line 32
def __add_from_array__(array)
  { keys.first => array.__add__(values.first) }
end
__deep_copy__() click to toggle source

Make a deep copy of this hash.

@example Make a deep copy of the hash.

{ field: value }.__deep_copy__

@return [ Hash ] The copied hash.

# File lib/mongoid/criteria/queryable/extensions/hash.rb, line 102
def __deep_copy__
  {}.tap do |copy|
    each_pair do |key, value|
      copy.store(key, value.__deep_copy__)
    end
  end
end
__expand_complex__() click to toggle source

Get the object as expanded.

@example Get the object expanded.

obj.__expand_complex__

@return [ Hash ] The expanded hash.

# File lib/mongoid/criteria/queryable/extensions/hash.rb, line 130
def __expand_complex__
  replacement = {}
  each_pair do |key, value|
    replacement.merge!(key.__expr_part__(value.__expand_complex__))
  end
  replacement
end
__intersect__(object) click to toggle source

Add an object to a hash using the merge strategies.

@example Add an object to a hash.

{ field: value }.__intersect__({ field: other_value })

@param [ Hash ] object The other hash to intersect.

@return [ Hash ] The hash with object intersected.

# File lib/mongoid/criteria/queryable/extensions/hash.rb, line 44
def __intersect__(object)
  apply_strategy(:__intersect__, object)
end
__intersect_from_array__(array) click to toggle source

Merge this hash into the provided array.

@example Merge the hash into the array.

{ field: value }.__intersect_from_array__([ 1, 2 ])

@param [ Array ] array The array to intersect to.

@return [ Hash ] The merged hash.

# File lib/mongoid/criteria/queryable/extensions/hash.rb, line 56
def __intersect_from_array__(array)
  { keys.first => array.__intersect__(values.first) }
end
__intersect_from_object__(object) click to toggle source

Merge this hash into the provided object.

@example Merge the hash into the object.

{ field: value }.__intersect_from_object__([ 1, 2 ])

@param [ Object ] object The object to intersect to.

@return [ Hash ] The merged hash.

# File lib/mongoid/criteria/queryable/extensions/hash.rb, line 68
def __intersect_from_object__(object)
  { keys.first => object.__intersect__(values.first) }
end
__sort_option__() click to toggle source

Get the hash as a sort option.

@example Get the hash as a sort option.

{ field: 1 }.__sort_option__

@return [ Hash ] The hash as sort option.

# File lib/mongoid/criteria/queryable/extensions/hash.rb, line 116
def __sort_option__
  tap do |hash|
    hash.each_pair do |key, value|
      hash.store(key, Mongoid::Criteria::Translator.to_direction(value))
    end
  end
end
__union__(object) click to toggle source

Add an object to a hash using the merge strategies.

@example Add an object to a hash.

{ field: value }.__union__({ field: other_value })

@param [ Hash ] object The other hash to union.

@return [ Hash ] The hash with object unioned.

# File lib/mongoid/criteria/queryable/extensions/hash.rb, line 80
def __union__(object)
  apply_strategy(:__union__, object)
end
__union_from_object__(object) click to toggle source

Merge this hash into the provided object.

@example Merge the hash into the object.

{ field: value }.__union_from_object__([ 1, 2 ])

@param [ Object ] object The object to union to.

@return [ Hash ] The merged hash.

# File lib/mongoid/criteria/queryable/extensions/hash.rb, line 92
def __union_from_object__(object)
  { keys.first => object.__union__(values.first) }
end

Private Instance Methods

apply_strategy(strategy, object) click to toggle source

Apply the provided strategy for the hash with the given object.

@api private

@example Apply the strategy.

{ field: value }.apply_strategy(:__add__, 1)

@param [ Symbol ] strategy The strategy to apply. @param [ Object ] object The object to merge.

@return [ Hash ] The merged hash.

# File lib/mongoid/criteria/queryable/extensions/hash.rb, line 151
def apply_strategy(strategy, object)
  tap do |hash|
    object.each_pair do |key, value|
      hash.store(key, hash[key].send(strategy, value))
    end
  end
end