Class: Configurate::Provider::StringHash

Inherits:
Base
  • Object
show all
Defined in:
lib/configurate/provider/string_hash.rb

Overview

This provider takes a nested string keyed hash and does nested lookups in it.

Direct Known Subclasses

TOML, YAML

Instance Method Summary collapse

Methods inherited from Base

#lookup

Constructor Details

#initialize(hash, namespace: nil, required: true, raise_on_missing: false, source: nil) ⇒ StringHash

Returns a new instance of StringHash.

Parameters:

  • hash (::Hash)

    the string keyed hash to provide values from

  • namespace (String) (defaults to: nil)

    optionally set this as the root

  • required (Boolean) (defaults to: true)

    whether or not to raise an error if the namespace, if given, is not found. Defaults to true.

  • raise_on_missing (Boolean) (defaults to: false)

    whether to raise MissingSetting if a setting can’t be provided. Defaults to false.

  • source (String) (defaults to: nil)

    optional hint of what’s the source of this configuration. Used in error messages.

Raises:

  • (ArgumentError)

    if the namespace isn’t found in the hash or the given object is not a hash



15
16
17
18
19
20
21
22
# File 'lib/configurate/provider/string_hash.rb', line 15

def initialize hash, namespace: nil, required: true, raise_on_missing: false, source: nil
  raise ArgumentError, "Please provide a hash" unless hash.is_a?(Hash)

  @required = required
  @raise_on_missing = raise_on_missing
  @source = source
  @settings = root_from hash, namespace
end

Instance Method Details

#lookup_path(setting_path, *_) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/configurate/provider/string_hash.rb', line 24

def lookup_path setting_path, *_
  Provider.lookup_in_hash(setting_path, @settings) {
    raise MissingSetting.new "#{setting_path} is not a valid setting." if @raise_on_missing

    nil
  }
end