Class: Mongoid::Criteria::Queryable::Key
- Inherits:
-
Object
- Object
- Mongoid::Criteria::Queryable::Key
- 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.
Instance Attribute Summary collapse
-
#block ⇒ Proc
readonly
The optional block to transform values.
-
#expanded ⇒ String
readonly
The MongoDB expanded query operator.
-
#name ⇒ String | Symbol
readonly
The name of the field.
-
#operator ⇒ String
readonly
The MongoDB query operator.
-
#strategy ⇒ Symbol
readonly
The name of the merge strategy.
Instance Method Summary collapse
-
#==(other) ⇒ true | false
(also: #eql?)
Does the key equal another object?.
-
#__expr_part__(object, negating = false) ⇒ Hash
Gets the raw selector that would be passed to Mongo from this key.
-
#__sort_option__ ⇒ Hash
(also: #__sort_pair__)
Get the key as raw Mongo sorting options.
-
#hash ⇒ Integer
Calculate the hash code for a key.
-
#initialize(name, strategy, operator, expanded = nil, &block) ⇒ Key
constructor
Instantiate the new key.
-
#to_s ⇒ String
Convert the key to a string.
-
#transform_value(value, negating = false) ⇒ Hash
Gets the raw selector condition that would be passed to Mongo.
Constructor Details
#initialize(name, strategy, operator, expanded = nil, &block) ⇒ Key
Instantiate the new key.
111 112 113 114 115 116 117 118 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 111 def initialize(name, strategy, operator, = 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, , block end |
Instance Attribute Details
#block ⇒ Proc (readonly)
Returns The optional block to transform values.
73 74 75 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 73 def block @block end |
#expanded ⇒ String (readonly)
Returns The MongoDB expanded query operator.
67 68 69 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 67 def @expanded end |
#name ⇒ String | Symbol (readonly)
Returns The name of the field.
61 62 63 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 61 def name @name end |
#operator ⇒ String (readonly)
Returns The MongoDB query operator.
64 65 66 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 64 def operator @operator end |
#strategy ⇒ Symbol (readonly)
Returns The name of the merge strategy.
70 71 72 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 70 def strategy @strategy end |
Instance Method Details
#==(other) ⇒ true | false Also known as: eql?
Does the key equal another object?
84 85 86 87 88 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 84 def ==(other) return false unless other.is_a?(Key) name == other.name && operator == other.operator && == other. end |
#__expr_part__(object, negating = false) ⇒ Hash
Gets the raw selector that would be passed to Mongo from this key.
129 130 131 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 129 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.
164 165 166 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 164 def __sort_option__ { name => operator } end |
#hash ⇒ Integer
Calculate the hash code for a key.
94 95 96 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 94 def hash [ name, operator, ].hash end |
#to_s ⇒ String
Convert the key to a string.
175 176 177 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 175 def to_s @name.to_s end |
#transform_value(value, negating = false) ⇒ Hash
Gets the raw selector condition that would be passed to Mongo.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 142 def transform_value(value, negating = false) expr = if block block[value] else value end expr = { => expr } if expr = { operator => expr } expr = { '$not' => expr } if negating && operator != '$not' expr end |