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

Inherits:
Object
  • Object
show all
Defined in:
build/mongoid-7.0/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 thusly:

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_spacial(: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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, strategy, operator, expanded = nil, &block) ⇒ Key

Instantiate the new key.

Examples:

Instantiate the key.

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

Parameters:

  • name (String, Symbol)

    The field name.

  • strategy (Symbol)

    The name of the merge strategy.

  • operator (String)

    The Mongo operator.

  • expanded (String) (defaults to: nil)

    The Mongo expanded operator.

Since:

  • 1.0.0



113
114
115
116
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/key.rb', line 113

def initialize(name, strategy, operator, expanded = nil, &block)
  @name, @strategy, @operator, @expanded, @block =
    name, strategy, operator, expanded, block
end

Instance Attribute Details

#blockProc (readonly)

Returns The optional block to transform values.

Returns:

  • (Proc)

    The optional block to transform values.



74
75
76
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/key.rb', line 74

def block
  @block
end

#expandedString (readonly)

Returns The MongoDB expanded query operator.

Returns:

  • (String)

    The MongoDB expanded query operator.



68
69
70
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/key.rb', line 68

def expanded
  @expanded
end

#nameString | Symbol (readonly)

Returns The name of the field.

Returns:

  • (String | Symbol)

    The name of the field.



62
63
64
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/key.rb', line 62

def name
  @name
end

#operatorString (readonly)

Returns The MongoDB query operator.

Returns:

  • (String)

    The MongoDB query operator.



65
66
67
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/key.rb', line 65

def operator
  @operator
end

#strategySymbol (readonly)

Returns The name of the merge strategy.

Returns:

  • (Symbol)

    The name of the merge strategy.



71
72
73
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/key.rb', line 71

def strategy
  @strategy
end

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.

Since:

  • 1.0.0



87
88
89
90
# File 'build/mongoid-7.0/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.

Since:

  • 1.0.0



129
130
131
132
133
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/key.rb', line 129

def __expr_part__(object, negating = false)
  value = block ? block[object] : object
  expression = { operator => expanded ? { expanded => value } : value }
  { name.to_s => (negating && operator != "$not") ? { "$not" => expression } : expression }
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.

Since:

  • 1.0.0



143
144
145
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/key.rb', line 143

def __sort_option__
  { name => operator }
end

#hashFixnum

Calculate the hash code for a key.

Returns:

  • (Fixnum)

    The hash code for the key.

Since:

  • 1.1.0



98
99
100
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/key.rb', line 98

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.

Since:

  • 1.1.0



156
157
158
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/key.rb', line 156

def to_s
  @name.to_s
end