Class: Precursor::ConfigRoot

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

Overview

Root class to access config vaults

Instance Method Summary collapse

Constructor Details

#initialize(vaults, key_options) ⇒ ConfigRoot



6
7
8
9
10
11
12
13
# File 'lib/config_root.rb', line 6

def initialize(vaults, key_options)
  @vaults = vaults
  @key_options = key_options

  @vaults.each do |v|
    v.load(self)
  end
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  key_vault = @vaults.find { |v| v.key? key }

  value = key_vault.value key unless key_vault.nil?
  value = get_default(key) if key_vault.nil?

  value.is_a?(String) ? resolve(value) : value
end

#key?(key) ⇒ Boolean

Tests if config value exist for a given key return [Boolean] True if value exists; false otherwise



27
28
29
# File 'lib/config_root.rb', line 27

def key?(key)
  @vaults.any? { |v| v.key? key }
end

#resolve(str) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/config_root.rb', line 31

def resolve(str)
  res = str
  until (m = res.match(VAR_PATTERN)).nil?
    k = m[1]
    v = self[k.to_sym]
    res = res.sub("${#{k}}", v.to_s)
  end
  res
end