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

Returns a new instance of 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

#resolve(str) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/config_root.rb', line 24

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