class A9n::Loader

Constants

COMMON_NAMESPACE
KNOWN_NAMESPACES

Attributes

env[R]
example_file[R]
local_file[R]
scope[R]
struct[R]

Public Class Methods

load_yml(file_path, scope, env) click to toggle source
# File lib/a9n/loader.rb, line 30
def load_yml(file_path, scope, env)
  return nil unless File.exist?(file_path)

  yml = A9n::YamlLoader.load(file_path)

  if no_known_namespaces?(yml)
    prepare_hash(yml, scope).freeze
  else
    common_namespace = prepare_hash(yml[COMMON_NAMESPACE], scope)
    env_namespace    = prepare_hash(yml[env], scope)

    A9n::Hash.merge(common_namespace, env_namespace).freeze
  end
end
new(file_path, scope, env) click to toggle source
# File lib/a9n/loader.rb, line 8
def initialize(file_path, scope, env)
  @scope = scope
  @env = ::A9n::StringInquirer.new(env.to_s)
  @local_file = file_path
  @example_file = "#{file_path}.example"
end
no_known_namespaces?(yml) click to toggle source
# File lib/a9n/loader.rb, line 51
def no_known_namespaces?(yml)
  (yml.keys & KNOWN_NAMESPACES).empty?
end
prepare_hash(data, scope) click to toggle source
# File lib/a9n/loader.rb, line 45
def prepare_hash(data, scope)
  return nil unless data.is_a?(::Hash)

  A9n::Hash.deep_prepare(data, scope).freeze
end

Public Instance Methods

get() click to toggle source
# File lib/a9n/loader.rb, line 15
def get
  struct || load
end
load() click to toggle source
# File lib/a9n/loader.rb, line 19
def load
  local_config    = self.class.load_yml(local_file, scope, env)
  example_config  = self.class.load_yml(example_file, scope, env)

  ensure_data_presence!(local_config, example_config)
  ensure_keys_presence!(local_config, example_config)

  @struct = A9n::Struct.new(local_config || example_config)
end

Private Instance Methods

ensure_data_presence!(local, example) click to toggle source
# File lib/a9n/loader.rb, line 58
def ensure_data_presence!(local, example)
  return unless local.nil?
  return unless example.nil?

  raise A9n::MissingConfigurationDataError, "Configuration data for *#{env}* env was not found in neither *#{example}* nor *#{local}*"
end
ensure_keys_presence!(local, example) click to toggle source
# File lib/a9n/loader.rb, line 65
def ensure_keys_presence!(local, example)
  return if local.nil?
  return if example.nil?

  missing_keys = example.keys - local.keys

  return if missing_keys.empty?

  raise A9n::MissingConfigurationVariablesError, "Following variables are missing in #{local_file} file: #{missing_keys.join(',')}"
end