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.



72
73
74
# File 'lib/king_konf/config.rb', line 72

def initialize(env: ENV)
  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



90
91
92
93
94
# File 'lib/king_konf/config.rb', line 90

def decode(name, value)
  decoded_value = self.class.variable(name).decode(value)

  set(name, decoded_value)
end

#get(name) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/king_konf/config.rb', line 81

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



76
77
78
79
# File 'lib/king_konf/config.rb', line 76

def load_file(path, environment = nil)
  loader = ConfigFileLoader.new(self)
  loader.load_file(path, environment)
end

#set(name, value) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/king_konf/config.rb', line 96

def set(name, value)
  unless self.class.variable?(name)
    raise ConfigError, "unknown configuration variable #{name}"
  end

  variable = self.class.variable(name)

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

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

  instance_variable_set("@#{name}", variable.cast(value))
end

#validate!Object



114
115
116
117
118
119
120
# File 'lib/king_konf/config.rb', line 114

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