类:Mongoid::Criteria::Queryable::Selector

继承:
Smash
  • 对象
显示全部
定义于:
lib/mongoid/criteria/queryable/selector.rb

Overview

选择器是一种特殊的哈希,它知道如何序列化进入其中的值,并了解键名称的别名和区域设置。

实例属性摘要

从Smash继承的属性

#aliased_associations 、 #aliased_associations aliased_associations。 、 #aliases 、 #aliases 别名。 、 #associations 、 #associations 关联。 、 #serializers 、 #serializers 序列化器。

实例方法摘要折叠

从Smash继承的方法

#[] 、 #__deep_copy__ 、 #initialize

构造函数详情

该类从Mongoid::Criteria::Queryable::Smash继承构造函数

实例方法详细信息

# merge! (其他)→选择器

将另一个选择器合并到此选择器中。

例子:

合并到另一个选择器中。

selector.merge!(name: "test")

参数:

  • 其他 (哈希 |选择器) —

    要合并的对象。

返回:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mongoid/criteria/queryable/selector.rb', line 17

def 合并!(其他)
  其他.each_pair do |key, 值|
    if 值.is_a?(哈希) && self[key.to_s].is_a?(哈希)
      值 = self[key.to_s].合并(merge)(值) do |_key, old_val, new_val|
        案例 _key.to_s
        when ' $in '
          new_val 和 old_val
        when '$nin'
          (old_val + new_val).uniq
        else
          new_val
        end
      end
    end
    值 = (self[key.to_s] || []).concat(值) if multi_selection?(key)
    存储(key, 值)
  end
end

# 存储 (key, value) ⇒ Object也称为: []=

将该值存储在所提供键的选择器中。 选择器将在此步骤中处理所有必要的序列化和本地化。

例子:

在选择器中存储一个值。

selector.store(:key, "testing")

参数:

  • key ( string | Symbol ) —

    属性的名称。

  • 值 ( Object ) —

    要添加的值。

返回:

  • ( Object ) —

    存储的对象。



46
47
48
49
50
51
52
53
54
55
# File 'lib/mongoid/criteria/queryable/selector.rb', line 46

def 存储(key, 值)
  名称, 序列化器 = storage_pair(key)
  if multi_selection?(名称)
    store_name = 名称
    store_value = evolve_multi(值)
  else
    store_name, store_value = store_creds(名称, 序列化器, 值)
  end
  超(store_name, store_value)
end

# to_pipeline ⇒ Array<Hash>

将选择器转换为聚合管道条目。

例子:

将选择器转换为管道。

selector.to_pipeline

返回:

  • ( Array<Hash> ) —

    选择器的管道条目。



64
65
66
67
68
# File 'lib/mongoid/criteria/queryable/selector.rb', line 64

def to_pipeline
  管道 = []
  管道.推动({ '$match' => self }) 除非 空?
  管道
end