123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Config::Options

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Defined in: lib/mongoid/config/options.rb

Overview

Encapsulates logic for setting options.

Instance Method Summary

Instance Method Details

#defaultsHash

Get the defaults or initialize a new empty hash.

Examples:

Get the defaults.

options.defaults

Returns:

  • (Hash)

    The default options.

[ GitHub ]

  
# File 'lib/mongoid/config/options.rb', line 16

def defaults
  @defaults ||= {}
end

#log_levelInteger

Get the log level.

Examples:

Get the log level.

config.log_level

Returns:

[ GitHub ]

  
# File 'lib/mongoid/config/options.rb', line 83

def log_level
  if level = settings[:log_level]
    unless level.is_a?(Integer)
      # JRuby String#constantize does not work here.
      level = Logger.const_get(level.upcase.to_s)
    end
    level
  end
end

#option(name, options = {})

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.

  • :on_change (Proc | nil)

    The callback to invoke when the setter is invoked.

[ GitHub ]

  
# File 'lib/mongoid/config/options.rb', line 31

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
      options[:on_change]&.call(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.

[ GitHub ]

  
# File 'lib/mongoid/config/options.rb', line 59

def reset
  # do this via the setter for each option, so that any defined on_change
  # handlers can be invoked.
  defaults.each do |setting, default|
    send(:"#{setting}=", default)
  end
end

#settingsHash

Get the settings or initialize a new empty hash.

Examples:

Get the settings.

options.settings

Returns:

  • (Hash)

    The setting options.

[ GitHub ]

  
# File 'lib/mongoid/config/options.rb', line 73

def settings
  @settings ||= {}
end