class Metrux::ConfigBuilders::Yaml

Constants

EnvironmentNotFoundError
FileLoadError

Attributes

config_path[R]
env[R]

Public Class Methods

new(config_path, env) click to toggle source
# File lib/metrux/config_builders/yaml.rb, line 7
def initialize(config_path, env)
  @config_path = config_path
  @env = env
end

Public Instance Methods

build() click to toggle source
# File lib/metrux/config_builders/yaml.rb, line 12
def build
  file_exists? ? yaml_content : null_content
end

Private Instance Methods

default_environment() click to toggle source
# File lib/metrux/config_builders/yaml.rb, line 52
def default_environment
  Configuration::DEFAULT_ENVIRONMENT
end
file_exists?() click to toggle source
# File lib/metrux/config_builders/yaml.rb, line 20
def file_exists?
  File.exist?(config_path)
end
from_environment(config_content) click to toggle source
# File lib/metrux/config_builders/yaml.rb, line 40
def from_environment(config_content)
  config_content.fetch(env)
rescue KeyError => e
  if env == default_environment
    raise(EnvironmentNotFoundError, "#{e.class}: #{e.message}")
  end

  warn_environment_change
  @env = default_environment
  retry
end
load_file(config_path) click to toggle source
# File lib/metrux/config_builders/yaml.rb, line 32
def load_file(config_path)
  content = File.read(config_path)
  template = ERB.new(content)
  YAML.load(template.result)
rescue => e
  raise(FileLoadError, "#{e.class}: #{e.message}")
end
null_content() click to toggle source
# File lib/metrux/config_builders/yaml.rb, line 24
def null_content
  {}
end
warn_environment_change() click to toggle source
# File lib/metrux/config_builders/yaml.rb, line 56
def warn_environment_change
  Kernel.warn(
    "[WARNING] Metrux's configuration wasn't found for environment "\
    "\"#{env}\". Switching to default: \"#{default_environment}\"."
  )
end
yaml_content() click to toggle source
# File lib/metrux/config_builders/yaml.rb, line 28
def yaml_content
  from_environment(load_file(config_path)).with_indifferent_access
end