类:Mongoid::PersistenceContext
Overview
用于设置/获取集合和数据库名称的对象封装逻辑,以及具有持久模型时使用的特定选项的客户端。
常量摘要折叠
- EXTRA_OPTIONS =
除驾驶员客户端选项之外用于确定持久性上下文的额外选项。
[ :client, :collection, :collection_options ].冻结
- VALID_OPTIONS =
有效持久性上下文选项的完整列表。
( mongo::客户::VALID_OPTIONS + EXTRA_OPTIONS ).冻结
实例属性摘要折叠
-
# options ⇒ 哈希
只读
定义此持久性上下文的选项。
类方法摘要折叠
-
。 clear (对象, 集群 = nil,original_context = nil) ⇒ 对象
清除特定类或模型实例的持久化上下文。
-
。 get (object) ⇒ Mongoid::PersistenceContext
获取特定类或模型实例的持久化上下文。
-
。 设立 (对象, options_or_context) ⇒ Mongoid::PersistenceContext
为特定的类或模型实例设置持久化上下文。
实例方法摘要折叠
-
# == (other) ⇒ true | false
确定此持久化上下文是否与另一个相同。
-
#客户端⇒ Mongo::Client
获取此持久性上下文的客户端。
-
#client_name ⇒ Symbol
获取此持久性上下文的客户端名称。
-
# 集合 (parent = nil) ⇒ Mongo::Collection
获取此持久性上下文的集合。
-
# collection_name ⇒ string
获取此持久性上下文的集合名称。
-
# database_name ⇒ string
获取此持久性上下文的数据库名称。
-
# for_child (文档) ⇒ PersistenceContext
private
返回与给定子文档一致的新持久性上下文,继承最合适的设置。
-
#initialize (object, opts = {}) ⇒ PersistenceContext
构造函数
初始化持久性上下文对象。
-
# requests_storage_options ⇒ 哈希 | nil
private
所提供选项的子集,可用作存储选项。
-
# reusable_client? ⇒ true | false
private
上下文的客户端是否可以稍后重用,因此不应关闭。
构造函数详情
#initialize (对象, opts = {}) ⇒ PersistenceContext
初始化持久性上下文对象。
46 47 48 49 |
# File 'lib/mongoid/persistence_context.rb', line 46 def 初始化(对象, opts = {}) 对象 = 对象 (opts) end |
实例属性详细信息
# options ⇒哈希(只读)
定义此持久性上下文的选项。
20 21 22 |
# File 'lib/mongoid/persistence_context.rb', line 20 def @options end |
类方法详细信息
。 clear (对象, 集群 = nil,original_context = 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
获取特定类或模型实例的持久化上下文。
258 259 260 |
# File 'lib/mongoid/persistence_context.rb', line 258 def 获取(对象) get_context(对象) end |
。 set (object, options_or_context) ⇒ 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 集(对象, ) existing_context = get_context(对象) = if existing_context existing_context. else {} end if .is_a?(PersistenceContext) = . end = .合并(merge)() 上下文 = PersistenceContext.new(对象, ) store_context(对象, 上下文) end |
实例方法详细信息
# == (other) ⇒ true | false
确定此持久化上下文是否与另一个相同。
154 155 156 157 |
# File 'lib/mongoid/persistence_context.rb', line 154 def ==(其他) return false 除非 其他.is_a?(PersistenceContext) == 其他. end |
#客户端⇒ 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) = if database_name_option 客户端 = 客户端.请使用(database_name) = .除了(:database, 'database') end 客户端 = 客户端.通过() 除非 .空? 客户端 end end |
#client_name ⇒ Symbol
获取此持久性上下文的客户端名称。
140 141 142 143 144 |
# File 'lib/mongoid/persistence_context.rb', line 140 def client_name @client_name ||= __evaluate__([:client]) || 线程化.client_override || __evaluate__([:client]) end |
# 集合 (parent = nil) ⇒ Mongo::Collection
获取此持久性上下文的集合。
81 82 83 84 85 |
# File 'lib/mongoid/persistence_context.rb', line 81 def 集合(父项 = nil) 父项 ? 父项.集合.通过(.除了(:database, " database ")) : 客户端[collection_name.to_sym] end |
# collection_name ⇒ string
获取此持久性上下文的集合名称。
94 95 96 97 |
# File 'lib/mongoid/persistence_context.rb', line 94 def collection_name @collection_name ||= (__evaluate__([:collection] || [:collection])) end |
# 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)(文档.)) end |
# requests_storage_options ⇒哈希 | nil
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
所提供选项的子集,可用作存储选项。
181 182 183 184 |
# File 'lib/mongoid/persistence_context.rb', line 181 def slice = @options.slice(*Mongoid::客户端::验证器::存储::VALID_OPTIONS) slice.任何? ? slice : nil end |
# reusable_client? ⇒ true | false
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
上下文的客户端是否可以稍后重用,因此不应关闭。
如果仅使用 : 客户端选项请求持久性上下文,则意味着上下文应使用 mongoid.yml 中配置的客户端。 清除上下文时不应关闭此类客户端,因为它们稍后会重复使用。
170 171 172 |
# File 'lib/mongoid/persistence_context.rb', line 170 def reusable_client? @options.密钥 == [:client] end |