class KingKonf::Config

Public Class Methods

env_prefix(prefix = nil) click to toggle source
# File lib/king_konf/config.rb, line 9
def env_prefix(prefix = nil)
  @env_prefix = prefix if prefix
  @env_prefix
end
ignore_unknown_variables(should_ignore) click to toggle source
# File lib/king_konf/config.rb, line 14
def ignore_unknown_variables(should_ignore)
  @ignore_unknown_variables = should_ignore
end
ignore_unknown_variables?() click to toggle source
# File lib/king_konf/config.rb, line 18
def ignore_unknown_variables?
  # Always ignore about unknown ENV vars if there's no prefix defined.
  @ignore_unknown_variables || !env_prefix
end
new(env: ENV) click to toggle source
# File lib/king_konf/config.rb, line 73
def initialize(env: ENV)
  @desc = nil
  @ignore_unknown_variables = nil
  load_env(env)
end
variable(name) click to toggle source
# File lib/king_konf/config.rb, line 23
def variable(name)
  @variables.fetch(name.to_s)
end
variable?(name) click to toggle source
# File lib/king_konf/config.rb, line 27
def variable?(name)
  @variables.key?(name.to_s)
end
variables() click to toggle source
# File lib/king_konf/config.rb, line 31
def variables
  @variables.values
end

Private Class Methods

desc(description) click to toggle source
# File lib/king_konf/config.rb, line 68
def desc(description)
  @desc = description
end

Public Instance Methods

decode(name, value) click to toggle source
# File lib/king_konf/config.rb, line 93
def decode(name, value)
  decoded_value = self.class.variable(name).decode(value)

  set(name, decoded_value)
end
get(name) click to toggle source
# File lib/king_konf/config.rb, line 84
def get(name)
  if instance_variable_defined?("@#{name}")
    instance_variable_get("@#{name}")
  else
    variable = self.class.variable(name)
    variable.default
  end
end
load_file(path, environment = nil) click to toggle source
# File lib/king_konf/config.rb, line 79
def load_file(path, environment = nil)
  loader = ConfigFileLoader.new(self)
  loader.load_file(path, environment)
end
set(name, value) click to toggle source
# File lib/king_konf/config.rb, line 99
def set(name, value)
  unless self.class.variable?(name)
    raise ConfigError, "unknown configuration variable #{name}"
  end

  variable = self.class.variable(name)

  cast_value = value.nil? ? nil : variable.cast(value)

  if !variable.allowed?(cast_value)
    raise ConfigError, "invalid value #{value.inspect} for variable `#{name}`, allowed values are #{variable.allowed_values}"
  end

  if !variable.valid?(value)
    raise ConfigError, "invalid value #{value.inspect} for variable `#{name}`"
  end

  instance_variable_set("@#{name}", cast_value)
end
validate!() click to toggle source
# File lib/king_konf/config.rb, line 119
def validate!
  self.class.variables.each do |variable|
    if variable.required? && get(variable.name).nil?
      raise ConfigError, "required variable `#{variable.name}` is not defined"
    end
  end
end

Private Instance Methods

load_config(config) click to toggle source
# File lib/king_konf/config.rb, line 129
def load_config(config)
  config.each do |variable, value|
    set(variable, value)
  end
end
load_env(env) click to toggle source
# File lib/king_konf/config.rb, line 135
def load_env(env)
  loaded_keys = []
  prefix = self.class.env_prefix ? "#{self.class.env_prefix.upcase}_" : ""

  self.class.variables.each do |variable|
    key = prefix + variable.name.upcase.to_s

    if string = env[key]
      value = variable.decode(string)
      set(variable.name, value)
      loaded_keys << key
    end
  end


  unless self.class.ignore_unknown_variables?
    env.keys.grep(/^#{prefix}/).each do |key|
      unless loaded_keys.include?(key)
        raise ConfigError, "unknown environment variable #{key}"
      end
    end
  end
end