Module: Mongoid::Config::Options

Included in:
Mongoid::Config
Defined in:
build/mongoid-master/lib/mongoid/config/options.rb

Overview

Encapsulates logic for setting options.

Instance Method Summary collapse

Instance Method Details

#defaultsHash

Get the defaults or initialize a new empty hash.

Examples:

Get the defaults.

options.defaults

Returns:

  • (Hash)

    The default options.

Since:

  • 2.3.0



18
19
20
# File 'build/mongoid-master/lib/mongoid/config/options.rb', line 18

def defaults
  @defaults ||= {}
end

#log_levelInteger

Get the log level.

Examples:

Get the log level.

config.log_level

Returns:

  • (Integer)

    The log level.

Since:

  • 5.1.0



86
87
88
89
90
91
92
93
94
# File 'build/mongoid-master/lib/mongoid/config/options.rb', line 86

def log_level
  if level = settings[:log_level]
    unless level.is_a?(Integer)
      level = level.upcase.to_s
      level = "Logger::#{level}".constantize
    end
    level
  end
end

#option(name, options = {}) ⇒ Object

Define a configuration option with a default.

Examples:

Define the option.

Options.option(:logger, :default => Logger.new(STDERR, :warn))

Parameters:

  • name (Symbol)

    The name of the configuration option.

  • options (Hash) (defaults to: {})

    Extras for the option.

Options Hash (options):

  • :default (Object)

    The default value.

Since:

  • 2.0.0.rc.1



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'build/mongoid-master/lib/mongoid/config/options.rb', line 33

def option(name, options = {})
  defaults[name] = settings[name] = options[:default]

  class_eval do
    # log_level accessor is defined specially below
    unless name.to_sym == :log_level
      define_method(name) do
        settings[name]
      end
    end

    define_method("#{name}=") do |value|
      settings[name] = value
    end

    define_method("#{name}?") do
      !!send(name)
    end
  end
end

#resetHash

Reset the configuration options to the defaults.

Examples:

Reset the configuration options.

config.reset

Returns:

  • (Hash)

    The defaults.

Since:

  • 2.3.0



62
63
64
# File 'build/mongoid-master/lib/mongoid/config/options.rb', line 62

def reset
  settings.replace(defaults)
end

#settingsHash

Get the settings or initialize a new empty hash.

Examples:

Get the settings.

options.settings

Returns:

  • (Hash)

    The setting options.

Since:

  • 2.3.0



74
75
76
# File 'build/mongoid-master/lib/mongoid/config/options.rb', line 74

def settings
  @settings ||= {}
end