Class: KingKonf::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/king_konf/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV) ⇒ Config

Returns a new instance of Config.


73
74
75
76
77
# File 'lib/king_konf/config.rb', line 73

def initialize(env: ENV)
  @desc = nil
  @ignore_unknown_variables = nil
  load_env(env)
end

Class Method Details

.env_prefix(prefix = nil) ⇒ Object


9
10
11
12
# 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) ⇒ Object


14
15
16
# File 'lib/king_konf/config.rb', line 14

def ignore_unknown_variables(should_ignore)
  @ignore_unknown_variables = should_ignore
end

.ignore_unknown_variables?Boolean

Returns:

  • (Boolean)

18
19
20
21
# 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

.variable(name) ⇒ Object


23
24
25
# File 'lib/king_konf/config.rb', line 23

def variable(name)
  @variables.fetch(name.to_s)
end

.variable?(name) ⇒ Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/king_konf/config.rb', line 27

def variable?(name)
  @variables.key?(name.to_s)
end

.variablesObject


31
32
33
# File 'lib/king_konf/config.rb', line 31

def variables
  @variables.values
end

Instance Method Details

#decode(name, value) ⇒ Object


93
94
95
96
97
# 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) ⇒ Object


84
85
86
87
88
89
90
91
# 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) ⇒ Object


79
80
81
82
# 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) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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!Object


119
120
121
122
123
124
125
# 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