模块:Mongoid::Config

扩展方式:
可转发、配置默认加密选项
包括:
DeprecatedOptions
包含在:
配置
定义于:
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

DeprecatedOptions中包含的常量

DeprecatedOptions::OPTIONS

实例方法摘要折叠

选项中包含的方法

defaultslog_leveloptionresetsettings

默认值中包含的方法

load_defaults

Encryption中包含的方法

encryption_schema_map

DeprecatedOptions中包含的方法

前缀

实例方法详细信息

# 个客户端哈希

获取客户端配置或空哈希。

例子:

获取客户端配置。

config.clients

返回:

  • (哈希)

    客户端配置。



368
369
370
# File 'lib/mongoid/config.rb', line 368

def 客户端
  @clients ||= {}
end

#configself

返回配置单例,以在配置 DSL 中使用。

返回:

  • ( self )

    配置单例。



186
187
188
# File 'lib/mongoid/config.rb', line 186

def config
  self
end

#已配置?true | false

Mongoid 是否已配置? 这是为了检查是否至少存在有效的客户端配置。

例子:

是否已配置 Mongoid?

config.configured?

返回:

  • ( true | false )

    如果配置了 Mongoid。



197
198
199
# File 'lib/mongoid/config.rb', line 197

def 已配置?
  客户端.键?(:default)
end

# connect_to (name, options = { read: { 模式: : 主节点 (primary node in the replica set) }}) ⇒对象

注意:

为方便起见,仅在开发或测试环境中使用。

在默认客户端上连接到提供的数据库名称。

例子:

设置要连接的数据库。

config.connect_to("mongoid_test")

参数:

  • 名称 ( string )

    数据库名称。



209
210
211
212
213
214
215
216
217
# File 'lib/mongoid/config.rb', line 209

def connect_to(名称, 选项 = { 读取: { 模式: :primary }})
  self.客户端 = {
    默认: {
      database: 名称,
      主机: [ " localhost: 27017 " ],
      选项: 选项
    }
  }
end

# deregister_model (klass) ⇒对象

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

使用 Mongoid 在应用程序中取消注册模型。

参数:

  • klass ( class )

    要取消注册的模型。



276
277
278
279
280
# File 'lib/mongoid/config.rb', line 276

def deregister_model(klass)
  .同步 do
    模型.删除(klass)
  end
end

# structural_fieldsArray<String>

如果在 Mongoid::Document 中定义,则返回可能导致破坏性事件发生的字段名称。

例子:

获取破坏性字段。

config.destructive_fields

返回:

  • ( Array<String> )

    错误字段名称的大量。



226
227
228
# File 'lib/mongoid/config.rb', line 226

def 破坏性字段
  可组合.Barceled_methods
end

# load! (path, environment = nil) ⇒对象

从兼容的 mongoid.yml文件加载设置。 这可用于轻松设置Rails 以外的框架。

例子:

配置 Mongoid。

Mongoid.load!("/path/to/mongoid.yml")

参数:

  • 路径 ( string )

    文件的路径。

  • environment string | 符号 (默认为: nil

    要加载的环境。



238
239
240
241
242
243
244
245
246
# File 'lib/mongoid/config.rb', line 238

def 加载!(路径, environment = nil)
  设置 = environment.load_yaml(路径, environment)
  if 设置.现在?
    客户端.断开连接
    客户端.清除
    load_configuration(设置)
  end
  设置
end

# load_configuration (settings) ⇒对象

从设置的哈希中加载所有配置。

例子:

加载配置。

config.load_configuration(settings)

参数:

  • 设置 (哈希)

    配置设置。



288
289
290
291
292
293
294
# File 'lib/mongoid/config.rb', line 288

def load_configuration(设置)
  配置 = 设置.with_indifference_access
  self.选项 = 配置[:options]
  self.客户端 = 配置[:clients]
  mongo.选项 = 配置[:driver_options] || {}
  set_log_levels
end

#modelsArray<Class>

获取应用程序中的所有模型,即包括 Mongoid::Document 在内的所有模型。

例子:

获取所有模型。

config.models

返回:

  • ( Array<Class> )

    应用程序中的所有模型。



255
256
257
# File 'lib/mongoid/config.rb', line 255

def 模型
  @models ||= []
end

# options= (options) ⇒对象

设置配置选项。 将单独验证每一项。

例子:

设置选项。

config.options = { raise_not_found_error: true }

参数:

  • 选项 (哈希)

    配置选项。



352
353
354
355
356
357
358
359
360
# File 'lib/mongoid/config.rb', line 352

def options=(选项)
  if 选项
    验证器::AsyncQueryExecutor.验证(选项)
    选项.each_pair do |选项, |
      验证器::选项.验证(选项)
      发送(" #{ option } = ", )
    end
  end
end

# override_client (name) ⇒ string |符号

覆盖客户端以在全局范围内使用。

例子:

全局覆盖客户端。

config.override_client(:optional)

参数:

  • 名称 ( string | Symbol )

    客户端的名称。

返回:

  • ( string | Symbol )

    全局覆盖。



316
317
318
# File 'lib/mongoid/config.rb', line 316

def override_client(名称)
  线程化.client_override = 名称 ? 名称.to_s : nil
end

# override_database (name) ⇒ string |符号

覆盖数据库以在全局范围内使用。

例子:

全局覆盖数据库。

config.override_database(:optional)

参数:

  • 名称 ( string | Symbol )

    数据库的名称。

返回:

  • ( string | Symbol )

    全局覆盖。



304
305
306
# File 'lib/mongoid/config.rb', line 304

def override_database(名称)
  线程化.database_override = 名称
end

# purge!true

注意:

这是删除所有数据的最快方法。

清除所有集合中的所有数据,包括索引。

例子:

清除所有数据。

Mongoid::Config.purge!

返回:

  • ( true )

    true。



328
329
330
# File 'lib/mongoid/config.rb', line 328

def purge!
  global_client.database.collections.(:drop)  true
end

#register_model(klass) ⇒ Object

在使用 Mongoid 的应用程序中注册模型。

例子:

注册模型。

config.register_model(Band)

参数:

  • klass ( class )

    要注册的模型。



265
266
267
268
269
# File 'lib/mongoid/config.rb', line 265

def register_model(klass)
  .同步 do
    模型.推动(klass) 除非 模型.包括?(klass)
  end
end

# running_with_passenger?true | false

已弃用。

应用程序是否在 Passenger 下运行?

例子:

应用程序是否正在使用 Passenger?

config.running_with_passenger?

返回:

  • ( true | false )

    如果应用部署在 Passenger 上。



390
391
392
# File 'lib/mongoid/config.rb', line 390

def running_with_passenger?
  @running_with_passenger ||= 定义?(PphsionPassenger)
end

#time_zoneString

获取要使用的时区域。

例子:

获取时区域。

Config.time_zone

返回:

  • ( string )

    时区域。



378
379
380
# File 'lib/mongoid/config.rb', line 378

def time_zone
  use_utc? ? " UTC " : ::时间.区域
end

#截断!true

注意:

这会比清除慢!

截断所有集合中的所有数据,但不截断索引。

例子:

截断所有集合数据。

Mongoid::Config.truncate!

返回:

  • ( true )

    true。



340
341
342
343
344
# File 'lib/mongoid/config.rb', line 340

def 截断!
  global_client.database.collections. do |集合|
    集合.find.delete_many
  end  true
end