模块:Mongoid::Config
- 包含在:
- 配置
- 定义于:
- lib/mongoid/config.rb ,
lib/mongoid/config/options.rb,
lib/mongoid/config/defaults.rb,
lib/mongoid/config/ 加密.rb,
lib/mongoid/config/environment.rb,
lib/mongoid/config/validators/ 客户端.rb,
lib/mongoid/config/validators/option.rb,
lib/mongoid/config/validators/async_query_executor.rb
Overview
该模块定义了 Mongoid 的所有配置选项,包括数据库连接。
在命名空间下定义
常量摘要折叠
- LOCK =
互斥锁.new
- VALID_ISOLATION_LEVELS =
%i[ Rails 线程 纤维 ].冻结
DeprecatedOptions中包含的常量
实例方法摘要折叠
-
# 个客户端⇒ 哈希
获取客户端配置或空哈希。
-
#config ⇒ self
返回配置单例,以在配置 DSL 中使用。
-
#已配置? ⇒ true | false
Mongoid 是否已配置? 这是为了检查是否至少存在有效的客户端配置。
-
#connect_to(name, options = { read: { 模式:: 主节点 (primary node in the replica set) } }) ⇒ 对象
在默认客户端上连接到提供的数据库名称。
-
#deregister_model(klass) ⇒ Object
private
使用 Mongoid 在应用程序中取消注册模型。
-
# structural_fields ⇒ Array<String>
如果在 Mongoid::Document 中定义,则返回可能导致破坏性事件发生的字段名称。
-
# load! (path, environment = nil) ⇒ 对象
从兼容的 mongoid.yml文件加载设置。
-
# load_configuration (settings) ⇒ 对象
从设置的哈希中加载所有配置。
-
#models ⇒ Array<Class>
获取应用程序中的所有模型,即包括 Mongoid::Document 在内的所有模型。
-
# options= (options) ⇒ 对象
设置配置选项。
-
# override_client (name) ⇒ string |符号
覆盖客户端以在全局范围内使用。
-
# override_database (name) ⇒ string |符号
覆盖数据库以在全局范围内使用。
-
# purge! ⇒ true
清除所有集合中的所有数据,包括索引。
-
#real_isolation_level ⇒ 对象
private
返回 Mongoid 用于存储其内部状态的(可能解除引用的)隔离性级别。
-
#register_model (klass) ⇒ 对象
在使用 Mongoid 的应用程序中注册模型。
- # running_with_passenger? ⇒ true | false 已弃用 已弃用。
-
# time_zone ⇒ string
获取要使用的时区域。
-
#截断! ⇒ true
截断所有集合中的所有数据,但不截断索引。
-
#validate_isolation_level!(关卡)→ 对象
private
检查 Mongoid 是否支持所提供的隔离性级别。
选项中包含的方法
defaults 、 log_level 、 option 、 reset 、 settings
默认值中包含的方法
Encryption中包含的方法
DeprecatedOptions中包含的方法
实例方法详细信息
# 个客户端⇒哈希
获取客户端配置或空哈希。
482 483 484 |
# File 'lib/mongoid/config.rb', line 482 def 客户端 @clients ||= {} end |
#config ⇒ self
返回配置单例,以在配置 DSL 中使用。
300 301 302 |
# File 'lib/mongoid/config.rb', line 300 def config self end |
#已配置? ⇒ true | false
Mongoid 是否已配置? 这是为了检查是否至少存在有效的客户端配置。
311 312 313 |
# File 'lib/mongoid/config.rb', line 311 def 已配置? 客户端.键?(:default) end |
# connect_to (name, options = { read: { 模式:: 主节点 (primary node in the replica set) } }) ⇒ 对象
为方便起见,仅在开发或测试环境中使用。
在默认客户端上连接到提供的数据库名称。
323 324 325 326 327 328 329 330 331 |
# File 'lib/mongoid/config.rb', line 323 def connect_to(名称, = { 读取: { 模式: :primary } }) self.客户端 = { 默认: { database: 名称, 主机: [ 'localhost:27017' ], 选项: } } end |
# deregister_model (klass) ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
使用 Mongoid 在应用程序中取消注册模型。
390 391 392 393 394 |
# File 'lib/mongoid/config.rb', line 390 def deregister_model(klass) 锁.同步 do 模型.删除(klass) end end |
# structural_fields ⇒ Array<String>
如果在 Mongoid::Document 中定义,则返回可能导致破坏性事件发生的字段名称。
340 341 342 |
# File 'lib/mongoid/config.rb', line 340 def 破坏性字段 可组合.Barceled_methods end |
# load! (path, environment = nil) ⇒对象
从兼容的 mongoid.yml文件加载设置。 这可用于轻松设置Rails 以外的框架。
352 353 354 355 356 357 358 359 360 |
# File 'lib/mongoid/config.rb', line 352 def 加载!(路径, environment = nil) 设置 = environment.load_yaml(路径, environment) if 设置.现在? 客户端.断开连接 客户端.清除 load_configuration(设置) end 设置 end |
# load_configuration (settings) ⇒对象
从设置的哈希中加载所有配置。
402 403 404 405 406 407 408 |
# File 'lib/mongoid/config.rb', line 402 def load_configuration(设置) 配置 = 设置.with_indifference_access self. = 配置[:options] self.客户端 = 配置[:clients] mongo. = 配置[:driver_options] || {} set_log_levels end |
#models ⇒ Array<Class>
获取应用程序中的所有模型,即包括 Mongoid::Document 在内的所有模型。
369 370 371 |
# File 'lib/mongoid/config.rb', line 369 def 模型 @models ||= [] end |
# options= (options) ⇒对象
设置配置选项。 将单独验证每一项。
466 467 468 469 470 471 472 473 474 |
# File 'lib/mongoid/config.rb', line 466 def () return 除非 验证器::AsyncQueryExecutor.验证() .each_pair do |选项, 值| 验证器::选项.验证(选项) 发送(" #{ option } = ", 值) end end |
# override_client (name) ⇒ string |符号
覆盖客户端以在全局范围内使用。
430 431 432 |
# File 'lib/mongoid/config.rb', line 430 def override_client(名称) 线程化.client_override = 名称 ? 名称.to_s : nil end |
# override_database (name) ⇒ string |符号
覆盖数据库以在全局范围内使用。
418 419 420 |
# File 'lib/mongoid/config.rb', line 418 def override_database(名称) 线程化.database_override = 名称 end |
# purge! ⇒ true
这是删除所有数据的最快方法。
清除所有集合中的所有数据,包括索引。
442 443 444 |
# File 'lib/mongoid/config.rb', line 442 def purge! global_client.database.collections.每(和:drop) 和 true end |
#real_isolation_level ⇒ 对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
返回 Mongoid 用于存储其内部状态的(可能解除引用的)隔离性级别。如果将 isolation_level设立为 :rails,则返回 Rails 当前配置使用的隔离性级别 (ActiveSupport::IsolatedExecutionState.isolation_level)。
如果使用不支持ActiveSupport::IsolatedExecutionState 的旧版本 Rails,则返回 :thread。
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/mongoid/config.rb', line 172 def real_isolation_level return Isolation_level 除非 Isolation_level == :rails if 定义?(ActiveSupport::IndependentExecutionState) ActiveSupport::IndependentExecutionState.Isolation_level.点击 do |等级| # 我们不能保证 Rails 始终支持相同的 #隔离性级别为 Mongoid,因此我们在此处进行检查以确保 # 这是我们可以使用的东西。 validate_isolation_level!(等级) end else #默认,如果 Rails 不支持IndependentExecutionState, :thread end end |
#register_model(klass) ⇒ Object
在使用 Mongoid 的应用程序中注册模型。
379 380 381 382 383 |
# File 'lib/mongoid/config.rb', line 379 def register_model(klass) 锁.同步 do 模型.推动(klass) 除非 模型.包括?(klass) end end |
# running_with_passenger? ⇒ true | false
应用程序是否在 Passenger 下运行?
504 505 506 |
# File 'lib/mongoid/config.rb', line 504 def running_with_passenger? @running_with_passenger ||= 定义?(PphsionPassenger) end |
#time_zone ⇒ String
获取要使用的时区域。
492 493 494 |
# File 'lib/mongoid/config.rb', line 492 def time_zone use_utc? ? 'UTC' : ::时间.区域 end |
#截断! ⇒ true
这会比清除慢!
截断所有集合中的所有数据,但不截断索引。
454 455 456 457 458 |
# File 'lib/mongoid/config.rb', line 454 def 截断! global_client.database.collections.每 do |集合| 集合.find.delete_many end 和 true end |
#validate_isolation_level!(关卡)→ 对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
检查 Mongoid 是否支持所提供的隔离性级别。如果不是,则引发 Errors::UnsupportedIsolationLevel。
如果隔离性级别设立为 :fiber 且Ruby版本低于 3.2,这也会引发错误,因为早期Ruby版本不支持纤程本地存储。
196 197 198 199 200 201 202 |
# File 'lib/mongoid/config.rb', line 196 def validate_isolation_level!(等级) 提高 Errors::UnsupportedIsolationLevel.new(等级) 除非 VALID_ISOLATION_LEVELS.包括?(等级) return 除非 等级 == :Fiber && RUBY_VERSION < ' 3.2 ' 提高 Errors::UnsupportedIsolationLevel.new(等级) end |