Class: Configurate::LookupChain

Inherits:
Object
  • Object
show all
Defined in:
lib/configurate/lookup_chain.rb

Overview

This object builds a chain of configuration providers to try to find the value of a setting.

Instance Method Summary collapse

Constructor Details

#initializeLookupChain

Returns a new instance of LookupChain.



5
6
7
# File 'lib/configurate/lookup_chain.rb', line 5

def initialize
  @provider = []
end

Instance Method Details

#add_provider(provider, *args) ⇒ void

This method returns an undefined value.

Adds a provider to the chain. Providers are tried in the order they are added, so the order is important.

Parameters:

  • provider (#lookup)
  • *args

    the arguments passed to the providers constructor

Raises:

  • (ArgumentError)

    if an invalid provider is given



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

def add_provider(provider, *args)
  unless provider.respond_to?(:instance_methods) && provider.instance_methods.include?(:lookup)
    raise ArgumentError, "the given provider does not respond to lookup"
  end

  @provider << provider.new(*args)
end

#lookup(setting, *args) ⇒ Array, ... Also known as: []

Tries all providers in the order they were added to provide a response for setting.

Parameters:

  • setting (SettingPath, String)

    nested settings as strings should be separated by a dot

  • ...

    further args passed to the provider

Returns:

  • (Array, Hash, String, Boolean, nil)

    whatever the responding provider provides is casted to a String, except for some special values



32
33
34
35
36
37
38
39
40
41
# File 'lib/configurate/lookup_chain.rb', line 32

def lookup(setting, *args)
  setting = SettingPath.new setting if setting.is_a? String
  @provider.each do |provider|
    begin
      return special_value_or_string(provider.lookup(setting.clone, *args))
    rescue SettingNotFoundError; end
  end

  nil
end