123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Config::Introspection Private

Do not use. This module is for internal use only.
Relationships & Source Files
Namespace Children
Classes:
Defined in: lib/mongoid/config/introspection.rb

Overview

This module provides a way to inspect not only the defined configuration settings and their defaults (which are available via Mongoid::Config.settings), but also the documentation about them. It does this by scraping the mongoid/config.rb file with a regular expression to match comments with options.

Constant Summary

  • CONFIG_RB_PATH =

    The full path to the source file of the ::Mongoid::Config module.

    # File 'lib/mongoid/config/introspection.rb', line 131
    File.absolute_path(File.join(
    File.dirname(__FILE__), "../config.rb"))
  • OPTION_PATTERN =

    A regular expression that looks for option declarations of the format:

    # one or more lines of comments,
    # followed immediately by an option
    # declaration with a default value:
    option :option_name, default: "something"

    The regex produces three captures:

    1: the (potentially multiline) comment
    2: the option's name
    3: the option's default value
    # File 'lib/mongoid/config/introspection.rb', line 120
    %r{
      (
        ((?:^\s*\#.*\n)+)  # match one or more lines of comments
        ^\s+option\s+      # followed immediately by a line declaring an option
        :(\w+),\s+         # match the option's name, followed by a comma
        default:\s+(.*?)   # match the default value for the option
        (?:,.*?)?          # skip any other configuration
      \n)                  # end with a newline
    }x

Instance Method Summary

Instance Method Details

#options(include_deprecated: false) ⇒ Array<Introspection::Option>

Extracts the available configuration options from the ::Mongoid::Config source file, and returns them as an array of hashes.

Parameters:

  • include_deprecated (true | false)

    Whether deprecated options should be included in the list. (Default: false)

Returns:

  • (Array<Introspection::Option>)

    ] the array of option objects representing each defined option, in alphabetical order by name.

[ GitHub ]

  
# File 'lib/mongoid/config/introspection.rb', line 142

def options(include_deprecated: false)
  src = File.read(CONFIG_RB_PATH)
  src.scan(OPTION_PATTERN)
    .map { |opt| Option.from_captures(opt) }
    .reject { |opt| !include_deprecated && opt.deprecated? }
    .sort_by { |opt| opt.name }
end