123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Criteria::Queryable::Key

Relationships & Source Files
Inherits: Object
Defined in: lib/mongoid/criteria/queryable/key.rb

Overview

Key objects represent specifications for building query expressions utilizing MongoDB selectors.

Simple key-value conditions are translated directly into expression hashes by ::Mongoid without utilizing Key objects. For example, the following condition:

Foo.where(price: 1)

… is translated to the following simple expression:

{price: 1}

More complex conditions would start involving Key objects. For example:

Foo.where(:price.gt => 1)

… causes a Key instance to be created as follows:

Key.new(:price, :__override__, '$gt')

This Key instance utilizes #operator but not #expanded nor #block. The corresponding MongoDB query expression is:

{price: {'$gt' => 1}}

A yet more more complex example is the following condition:

Foo.geo_spatial(:boundary.intersects_point => [1, 10])

Processing this condition will cause a Key instance to be created as follows:

Key.new(:location, :__override__, '$geoIntersects', '$geometry') do |value|
  { "type" => POINT, "coordinates" => value }
end

… eventually producing the following MongoDB query expression:

{

boundary: {
  '$geoIntersects' => {
    '$geometry' => {
      type: "Point" ,
      coordinates: [ 1, 10 ]
    }
  }
}

}

Key instances can be thought of as procs that map a value to the MongoDB query expression required to obtain the key’s condition, given the value.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name, strategy, operator, expanded = nil, &block) ⇒ Key

Instantiate the new key.

Examples:

Instantiate a key.

Key.new("age", :__override__, "$gt")

Instantiate a key for sorting.

Key.new(:field, :__override__, 1)

Parameters:

  • name (String | Symbol)

    The field name.

  • strategy (Symbol)

    The name of the merge strategy.

  • operator (String | Integer)

    The MongoDB operator, or sort direction (1 or -1).

  • expanded (String) (defaults to: nil)

    The Mongo expanded operator.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 113

def initialize(name, strategy, operator, expanded = nil, &block)
  unless operator.is_a?(String) || operator.is_a?(Integer)
    raise ArgumentError, "Operator must be a string or an integer: #{operator.inspect}"
  end

  @name, @strategy, @operator, @expanded, @block =
    name, strategy, operator, expanded, block
end

Instance Attribute Details

#blockProc (readonly)

Returns:

  • (Proc)

    The optional block to transform values.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 76

attr_reader :block

#expandedString (readonly)

Returns:

  • (String)

    The MongoDB expanded query operator.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 70

attr_reader :expanded

#nameString | Symbol (readonly)

Returns:

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 64

attr_reader :name

#operatorString (readonly)

Returns:

  • (String)

    The MongoDB query operator.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 67

attr_reader :operator

#strategySymbol (readonly)

Returns:

  • (Symbol)

    The name of the merge strategy.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 73

attr_reader :strategy

Instance Method Details

#==(other) ⇒ true | false Also known as: #eql?

Does the key equal another object?

Examples:

Is the key equal to another?

key == other
key.eql? other

Parameters:

  • other (Object)

    The object to compare to.

Returns:

  • (true | false)

    If the objects are equal.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 87

def ==(other)
  return false unless other.is_a?(Key)
  name == other.name && operator == other.operator && expanded == other.expanded
end

#__expr_part__(object, negating = false) ⇒ Hash

Gets the raw selector that would be passed to Mongo from this key.

Examples:

Specify the raw selector.

key.__expr_part__(50)

Parameters:

  • object (Object)

    The value to be included.

  • negating (true | false) (defaults to: false)

    If the selection should be negated.

Returns:

  • (Hash)

    The raw MongoDB selector.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 131

def __expr_part__(object, negating = false)
  { name.to_s => transform_value(object, negating) }
end

#__sort_option__Hash Also known as: #__sort_pair__

Get the key as raw Mongo sorting options.

Examples:

Get the key as a sort.

key.__sort_option__

Returns:

  • (Hash)

    The field/direction pair.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 170

def __sort_option__
  { name => operator }
end

#__sort_pair__

Alias for Mongoid::Criteria::Queryable::Key#sort_option.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 173

alias :__sort_pair__ :__sort_option__

#eql?(other)

Alias for #==.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 91

alias :eql? :==

#hashInteger

Calculate the hash code for a key.

Returns:

  • (Integer)

    The hash code for the key.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 96

def hash
  [name, operator, expanded].hash
end

#to_sString

Convert the key to a string.

Examples:

Convert the key to a string.

key.to_s

Returns:

  • (String)

    The key as a string.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 181

def to_s
  @name.to_s
end

#transform_value(value, negating = false) ⇒ Hash

Gets the raw selector condition that would be passed to Mongo.

Examples:

Gets the raw selector condition.

key.transform_value(50)

Parameters:

  • value (Object)

    The value to be included.

  • negating (true | false) (defaults to: false)

    If the selection should be negated.

Returns:

  • (Hash)

    The raw MongoDB selector.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/key.rb', line 144

def transform_value(value, negating = false)
  if block
    expr = block[value]
  else
    expr = value
  end

  if expanded
    expr = {expanded => expr}
  end

  expr = {operator => expr}

  if negating && operator != '$not'
    expr = {'$not' => expr}
  end

  expr
end