class SecretConfig::Parser

Attributes

interpolator[R]
path[R]
registry[R]
tree[R]

Public Class Methods

new(path, registry, interpolate: true) click to toggle source
# File lib/secret_config/parser.rb, line 5
def initialize(path, registry, interpolate: true)
  @path         = path
  @registry     = registry
  @fetch_list   = {}
  @import_list  = {}
  @tree         = {}
  @interpolator = interpolate ? SettingInterpolator.new : nil
end

Public Instance Methods

parse(key, value) click to toggle source

Returns a flat path of keys and values from the provider without looking in the local path. Keys are returned with path names relative to the supplied path.

# File lib/secret_config/parser.rb, line 16
def parse(key, value)
  relative_key       = relative_key?(key) ? key : key.sub("#{path}/", "")
  value              = interpolator.parse(value) if interpolator && value.is_a?(String) && value.include?("${")
  tree[relative_key] = value
end
render() click to toggle source

Returns a flat Hash of the rendered paths.

# File lib/secret_config/parser.rb, line 23
def render
  apply_imports if interpolator
  tree
end

Private Instance Methods

apply_imports() click to toggle source

Import from the current registry as well as new fetches.

Notes:

  • A lot of absolute key lookups can be expensive since each one is a separate call.

  • Imports cannot reference other imports at this time.

# File lib/secret_config/parser.rb, line 39
def apply_imports
  tree.keys.each do |key|
    next unless (key =~ %r{/__import__\Z}) || (key == "__import__")

    import_key = tree.delete(key)
    key, = ::File.split(key)
    key = nil if key == "."

    # binding.irb

    # With a relative key, look for the values in the current registry.
    # With an absolute key call the provider and fetch the value directly.

    if relative_key?(import_key)
      tree.keys.each do |current_key|
        match = current_key.match(%r{\A#{import_key}/(.*)})
        next unless match

        imported_key       = key.nil? ? match[1] : ::File.join(key, match[1])
        tree[imported_key] = tree[current_key] unless tree.key?(imported_key)
      end
    else
      relative_paths = registry.send(:fetch_path, import_key)
      relative_paths.each_pair do |relative_key, value|
        imported_key       = key.nil? ? relative_key : ::File.join(key, relative_key)
        tree[imported_key] = value unless tree.key?(imported_key)
      end
    end
  end
end
relative_key?(key) click to toggle source

Returns [true|false] whether the supplied key is considered a relative key.

# File lib/secret_config/parser.rb, line 71
def relative_key?(key)
  !key.start_with?("/")
end