Repository: rails/rails
File: activesupport/lib/active_support/encrypted_configuration.rb (Ruby)

Code before:
        cause.is_a?(Psych::SyntaxError) ? "#{super}\n\n  #{cause.message}" : super
      end
    end

    class InvalidKeyError < RuntimeError
      def initialize(content_path, key)
        super "Key '#{key}' is invalid, it must respond to '#to_sym' from configuration in '#{content_path}'."
      end
    end

    delegate_missing_to :options

    def initialize(config_path:, key_path:, env_key:, raise_if_missing_key:)
      super content_path: config_path, key_path: key_path,
        env_key: env_key, raise_if_missing_key: raise_if_missing_key
      @config = nil
      @options = nil
    end

    # Find singular or nested keys.
    # Raises +KeyError+ if not found or nil.
    #
    # Examples:
    #
    #   require(:db_host)         # => dig(:db_host) || raise(KeyError)
    #   require(:database, :host) # => dig(:database, :host) || raise(KeyError)
    def require(*key)
      value = dig(*key)

      if !value.nil?
        value
      else
        raise(KeyError)
      end
    end

    # Find singular or nested keys.
    # Returns +nil+ if the key isn't found.
    # If a +default+ value is defined, it (or its callable value) will be returned on a missing key or nil value.
    #
    # Examples:
    #
    #   config.option(:db_host)                                    # => dig(:db_host)
    #   config.option(:database, :host)                            # => dig(:database, :host)
    #   config.option(:database, :host, default: "missing")        # => dig(:database, :host) || "missing"
    #   config.option(:database, :host, default: -> { "missing" }) # => dig(:database, :host) || default.call
    def option(*key, default: nil)
      value = dig(*key)

      if !value.nil?
        value

Review comment (refactor):
I think the comments in here were a copy-paste leftover from `env_configuration.rb`?

I dunno if what I put here is that helpful, and it isn't 100% correct. E.g. the comment doesn't accurately show what happens when the value is `false`.
To be fully accurate we'd have to duplicate the method code in the comment.

I wonder if it might be better for the comments here and in the other two files to instead lay out a mini scenario and then show the results?

E.g. maybe something like:
```ruby
# Examples:
#
# db_host: "db.example.com"
# database:
#   host: "db.example.com"
#
# require(:db_host)         # => "db.example.com"
# require(:database, :host) # => "db.example.com"
# require(:missing)         # => KeyError
```
?

Code after:
        cause.is_a?(Psych::SyntaxError) ? "#{super}\n\n  #{cause.message}" : super
      end
    end

    class InvalidKeyError < RuntimeError
      def initialize(content_path, key)
        super "Key '#{key}' is invalid, it must respond to '#to_sym' from configuration in '#{content_path}'."
      end
    end

    delegate_missing_to :options

    def initialize(config_path:, key_path:, env_key:, raise_if_missing_key:)
      super content_path: config_path, key_path: key_path,
        env_key: env_key, raise_if_missing_key: raise_if_missing_key
      @config = nil
      @options = nil
    end

    # Find singular or nested keys.
    # Raises +KeyError+ if not found or nil.
    #
    # Given configuration:
    #   db_port: null
    #   database:
    #     host: "db.example.com"
    #
    # Examples:
    #   require(:database, :host) # => "db.example.com"
    #   require(:missing)         # => KeyError
    #   require(:db_port)         # => KeyError (nil values are treated as missing)
    def require(*key)
      value = dig(*key)

      if !value.nil?
        value
      else
        raise KeyError, "Missing key: #{key.inspect}"
      end
    end

    # Find singular or nested keys.
    # Returns +nil+ if the key isn't found.
    # If a +default+ value is defined, it (or its callable value) will be returned on a missing key or nil value.
    #
    # Given configuration:
    #   db_port: null
    #   database:
    #     host: "db.example.com"
    #
    # Examples: