类:Mongoid::PersistenceContext

继承:
对象
  • 对象
显示全部
扩展方式:
可转发
定义于:
lib/mongoid/persistence_context.rb

Overview

用于设置/获取集合和数据库名称的对象封装逻辑,以及具有持久模型时使用的特定选项的客户端。

常量摘要折叠

EXTRA_OPTIONS =

除驾驶员客户端选项之外用于确定持久性上下文的额外选项。

返回:

  • ( Array<Symbol> )

    除客户端选项之外用于确定持久性上下文的额外的选项列表。

[ :client,
  :collection,
  :collection_options
].冻结
VALID_OPTIONS =

有效持久性上下文选项的完整列表。

返回:

  • ( Array<Symbol> )

    定义持久性上下文的选项的完整列表。

( mongo::客户::VALID_OPTIONS + EXTRA_OPTIONS ).冻结

实例属性摘要折叠

类方法摘要折叠

实例方法摘要折叠

构造函数详情

#initialize (对象, opts = {}) ⇒ PersistenceContext

初始化持久性上下文对象。

例子:

创建新的持久性上下文。

PersistenceContext.new(model, collection: 'other')

参数:

  • 对象 ( Object )

    应为其创建持久性上下文的类或模型实例。

  • opts 哈希 (默认为: {}

    持久性上下文选项。



46
47
48
49
# File 'lib/mongoid/persistence_context.rb', line 46

def 初始化(对象, opts = {})
  对象 = 对象
  set_options!(opts)
end

实例属性详细信息

# options哈希(只读)

定义此持久性上下文的选项。

返回:

  • (哈希)

    持久性上下文选项。



20
21
22
# File 'lib/mongoid/persistence_context.rb', line 20

def 选项
  @options
end

类方法详细信息

clear (对象, 集群 = nil,original_context = nil) ⇒对象

清除特定类或模型实例的持久化上下文。

例子:

清除类或模型实例的持久化上下文。

PersistenceContext.clear(model)

参数:

  • 对象 ( class | Object )

    类或模型实例。

  • 集群 ( Mongo::Cluster ) (默认为: nil

    使用此上下文之前的原始集群。

  • ORIGIN_CONTEXT ( Mongoid::PersistenceContext ) (默认为: nil

    在使用此上下文之前设立的原始持久性上下文。



271
272
273
274
275
276
277
278
279
# File 'lib/mongoid/persistence_context.rb', line 271

def 清除(对象, 集群 = nil, ORIGIN_CONTEXT = nil)
  if 上下文 = 获取(对象)
    除非 集群.nil? || 上下文.集群.等于?(集群)
      上下文.客户端.关闭 除非 上下文.reusable_client?
    end
  end
确保
  store_context(对象, ORIGIN_CONTEXT)
end

get (对象) ⇒ Mongoid::PersistenceContext

获取特定类或模型实例的持久化上下文。

例子:

获取类或模型实例的持久性上下文。

PersistenceContext.get(model)

参数:

  • 对象 ( Object )

    类或模型实例。

返回:



258
259
260
# File 'lib/mongoid/persistence_context.rb', line 258

def 获取(对象)
  get_context(对象)
end

set (object, options_or_context) ⇒ Mongoid::PersistenceContext

为特定的类或模型实例设置持久化上下文。

如果已经存在持久性上下文设立,则现有上下文中的选项将与为设立调用提供的选项相结合。

例子:

为类或模型实例设置持久化上下文。

PersistenceContext.set(model)

参数:

  • 对象 ( Object )

    类或模型实例。

  • options_or_context ( Hash | Mongoid::PersistenceContext )

    持久化选项或持久化上下文对象。

返回:



235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/mongoid/persistence_context.rb', line 235

def (对象, options_or_context)
  existing_context = get_context(对象)
  existing_options = if existing_context
    existing_context.选项
  else
    {}
  end
  if options_or_context.is_a?(PersistenceContext)
    options_or_context = options_or_context.选项
  end
  new_options = existing_options.合并(merge)(options_or_context)
  上下文 = PersistenceContext.new(对象, new_options)
  store_context(对象, 上下文)
end

实例方法详细信息

# == (other) ⇒ true | false

确定此持久化上下文是否与另一个相同。

例子:

比较两个持久化上下文。

context == other_context

参数:

  • 其他 ( Object )

    要与此对象进行比较的对象。

返回:

  • ( true | false )

    两个持久化上下文是否相等。



154
155
156
157
# File 'lib/mongoid/persistence_context.rb', line 154

def ==(其他)
  return false 除非 其他.is_a?(PersistenceContext)
  选项 == 其他.选项
end

#客户端Mongo::Client

获取此持久性上下文的客户端。

例子:

获取此持久性上下文的客户端。

context.client

返回:

  • ( Mongo::Client )

    此持久性上下文的客户端。



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/mongoid/persistence_context.rb', line 117

def 客户端
  @client ||= 开始
    客户端 = 客户端.with_name(client_name)
    选项 = client_options

    if database_name_option
      客户端 = 客户端.请使用(database_name)
      选项 = 选项.除了(:database, 'database')
    end

    客户端 = 客户端.通过(选项) 除非 选项.空?

    客户端
  end
end

#client_nameSymbol

获取此持久性上下文的客户端名称。

例子:

获取此持久性上下文的客户端名称。

context.client_name

返回:

  • (符号)

    此持久性上下文的客户端名称。



140
141
142
143
144
# File 'lib/mongoid/persistence_context.rb', line 140

def client_name
  @client_name ||= __evaluate__(选项[:client]) ||
                     线程化.client_override ||
                     __evaluate__(storage_options[:client])
end

# 集合 (parent = nil) ⇒ Mongo::Collection

获取此持久性上下文的集合。

例子:

获取此持久性上下文的集合。

context.collection

参数:

  • 父项 对象 (默认为: nil

    父对象,其集合名称用于代替此持久性上下文的集合名称。

返回:

  • ( Mongo::Collection )

    此持久性上下文的集合。



81
82
83
84
85
# File 'lib/mongoid/persistence_context.rb', line 81

def 集合(父项 = nil)
  父项 ?
    父项.集合.通过(client_options.除了(:database, " database ")) :
    客户端[collection_name.to_sym]
end

# collection_namestring

获取此持久性上下文的集合名称。

例子:

获取此持久性上下文的集合名称。

context.collection_name

返回:

  • ( string )

    此持久性上下文的集合名称。



94
95
96
97
# File 'lib/mongoid/persistence_context.rb', line 94

def collection_name
  @collection_name ||= (__evaluate__(选项[:collection] ||
                         storage_options[:collection]))
end

# database_namestring

获取此持久性上下文的数据库名称。

例子:

获取此持久性上下文的数据库名称。

context.database_name

返回:

  • ( string )

    此持久性上下文的数据库名称。



106
107
108
# File 'lib/mongoid/persistence_context.rb', line 106

def database_name
  __evaluate__(database_name_option) || 客户端.database.名称
end

# for_child (文档) ⇒ PersistenceContext

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回与给定子文档一致的新持久性上下文,继承最合适的设置。

参数:

返回:



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mongoid/persistence_context.rb', line 59

def for_child(文档)
  if 文档.is_a?()
    return self if 文档 == (对象.is_a?() ? 对象 : 对象.class)
  elsif 文档.is_a?(Mongoid::文档)
    return self if 文档.class == (对象.is_a?() ? 对象 : 对象.class)
  else
    提高 ArgumentError, '必须指定类或文档实例'
  end

  PersistenceContext.new(文档, 选项.合并(merge)(文档.storage_options))
end

# requests_storage_options哈希 | nil

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

所提供选项的子集,可用作存储选项。

返回:

  • ( Hash | nil )

    请求的存储选项,如果未指定,则为 nil。



181
182
183
184
# File 'lib/mongoid/persistence_context.rb', line 181

def requests_storage_options
  slice = @options.slice(*Mongoid::客户端::验证器::存储::VALID_OPTIONS)
  slice.任何? ? slice : nil
end

# reusable_client?true | false

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

上下文的客户端是否可以稍后重用,因此不应关闭。

如果仅使用 : 客户端选项请求持久性上下文,则意味着上下文应使用 mongoid.yml 中配置的客户端。 清除上下文时不应关闭此类客户端,因为它们稍后会重复使用。

返回:

  • ( true | false )

    如果客户端可以重复使用,则为 true,否则为 false。



170
171
172
# File 'lib/mongoid/persistence_context.rb', line 170

def reusable_client?
  @options.密钥 == [:client]
end