类:Mongoid::Criteria::Queryable::Key
- 继承:
-
对象
- 对象
- Mongoid::Criteria::Queryable::Key
- 定义于:
- lib/mongoid/criteria/queryable/key.rb
Overview
键对象表示利用MongoDB选择器构建查询表达式的规范。
简单的键值条件由 Mongoid 直接转换为表达式哈希,而无需使用 Key 对象。 例如,以下条件:
Foo.where(price: 1)
… 被转换为以下简单表达式:
{price: 1}
更复杂的条件将开始涉及 Key 对象。 示例:
Foo.where(:price.gt => 1)
… 导致创建一个 Key 实例,如下所示:
Key.new(:price, :__override__, '$gt')
此 Key实例使用operator
,但不使用expanded
或block
。 相应的MongoDB查询表达式为:
{price: {'$gt' => 1}}
更复杂的示例是以下条件:
Foo.geo_spatial(:boundary.intersects_point => [1, 10])
处理此条件将导致创建一个 Key实例,如下所示:
Key.new(:location, :__override__, '$geoIntersects', '$geometry') do |value|
{ "type" => POINT, "coordinates" => value }
end
…最终生成以下MongoDB查询表达式:
{
boundary: {
'$geoIntersects' => {
'$geometry' => {
type: "Point" ,
coordinates: [ 1, 10 ]
}
}
}
}
键实例可以被视为将值映射到在给定值的情况下获取键的条件所需的 MongoDB 查询表达式的过程。
实例属性摘要折叠
-
#block ⇒ Proc
只读
用于转换值的可选区块。
-
#扩展⇒ string
只读
MongoDB扩展查询运算符。
-
#名称⇒ string |符号
只读
字段的名称。
-
#操作符⇒ string
只读
MongoDB 查询操作符。
-
#策略⇒ 符号
只读
合并策略的名称。
实例方法摘要折叠
-
# == (other) ⇒ true | false (也:#eql?)
键是否等于另一个对象?
-
# __expr_part__ (对象, negating = false) ⇒ 哈希
获取从此键传递给mongo的原始选择器。
-
# __sort_option__ ⇒ 哈希(也:#__sort_pair__)
获取密钥作为原始mongo排序选项。
-
#哈希⇒ 整数
计算键的哈希码。
-
#initialize (name,strategy, 操作符,expanded=nil,&block) ⇒ Key
构造函数
实例化新密钥。
-
# to_s ⇒ string
将密钥转换为string 。
-
#transform_value (value, negating = false) ⇒ 哈希
获取将传递给mongo的原始选择器条件。
构造函数详情
#initialize (name,strategy, 操作符,expanded=nil,&block) ⇒ Key
实例化新密钥。
113 114 115 116 117 118 119 120 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 113 def 初始化(名称, 策略, 运算符, = nil, 和块) 除非 运算符.is_a?(字符串) || 运算符.is_a?(整型) 提高 ArgumentError, " Operator 必须是string 或整数: # { { operator.inspect } } " end @name, @strategy, @操作符, @expanded, 区块 = 名称, 策略, 运算符, , 块 end |
实例属性详细信息
# 区块 ⇒ Proc (只读)
返回用于转换值的可选区块。
76 77 78 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 76 def 块 区块 end |
#扩展⇒ string (只读)
返回 MongoDB 扩展查询运算符。
70 71 72 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 70 def @expanded end |
#名称⇒ string |符号(只读)
返回字段的名称。
64 65 66 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 64 def 名称 @name end |
#操作符⇒ string (只读)
返回MongoDB查询运算符。
67 68 69 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 67 def 运算符 @操作符 end |
#策略⇒符号(只读)
返回 合并策略的名称。
73 74 75 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 73 def 策略 @strategy end |
实例方法详细信息
# == (other) ⇒ true | false也称为: eql?
键是否等于另一个对象?
87 88 89 90 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 87 def ==(其他) return false 除非 其他.is_a?(键) 名称 == 其他.名称 && 运算符 == 其他.运算符 && == 其他. end |
# __expr_part__ (object, negating = false) ⇒哈希
获取从此键传递给mongo的原始选择器。
131 132 133 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 131 def __expr_part__(对象, 否定 = false) { 名称.to_s => transform_value(对象, 否定) } end |
# __sort_option__ ⇒哈希也称为: __sort_pair__
获取密钥作为原始mongo排序选项。
170 171 172 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 170 def __sort_option__ { 名称 => 运算符 } end |
#哈希⇒整数
计算键的哈希码。
96 97 98 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 96 def 哈希 [名称, 运算符, ].哈希 end |
# to_s ⇒ string
将密钥转换为string 。
181 182 183 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 181 def to_s @name.to_s end |
#transform_value (value, negating = false) ⇒哈希
获取将传递给mongo的原始选择器条件。
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/mongoid/criteria/queryable/key.rb', line 144 def transform_value(值, 否定 = false) if 块 expr = 块[值] else expr = 值 end if expr = { => expr} end expr = {运算符 => expr} if 否定 && 运算符 != ' $not ' expr = {' $not ' => expr} end expr end |