class SecretConfig::Providers::File

Read configuration from a local file

Attributes

file_name[R]

Public Class Methods

new(file_name: "config/application.yml") click to toggle source
# File lib/secret_config/providers/file.rb, line 10
def initialize(file_name: "config/application.yml")
  @file_name = file_name
  raise(ConfigurationError, "Cannot find config file: #{file_name}") unless ::File.exist?(file_name)
end

Public Instance Methods

each(path, &block) click to toggle source

Yields the key with its absolute path and corresponding string value

# File lib/secret_config/providers/file.rb, line 16
def each(path, &block)
  settings = fetch_path(path)

  raise(ConfigurationError, "Path #{paths.join('/')} not found in file: #{file_name}") unless settings

  Utils.flatten_each(settings, path, &block)
  nil
end
fetch(_key) click to toggle source

Returns the value or `nil` if not found

# File lib/secret_config/providers/file.rb, line 26
def fetch(_key)
  values = fetch_path(path)
  values.is_a?(Hash) ? nil : values
end

Private Instance Methods

fetch_path(path) click to toggle source
# File lib/secret_config/providers/file.rb, line 33
def fetch_path(path)
  config = YAML.load(ERB.new(::File.new(file_name).read).result)

  paths = path.sub(%r{\A/*}, "").sub(%r{/*\Z}, "").split("/")
  config.dig(*paths)
end