Class: Konfa::Base

Inherits:
Object
  • Object
show all
Includes:
Deprecation, Initializer
Defined in:
lib/konfa.rb

Class Method Summary collapse

Methods included from Deprecation

included

Methods included from Initializer

included

Class Method Details

.after_initializeObject



157
158
# File 'lib/konfa.rb', line 157

def after_initialize
end

.allowed_variablesObject



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

def allowed_variables
  {}
end

.dumpObject



106
107
108
# File 'lib/konfa.rb', line 106

def dump
  self.configuration.dup
end

.env_variable_prefixObject

The following methods should be overridden and used to configure the configuration class



68
69
70
# File 'lib/konfa.rb', line 68

def env_variable_prefix
  'APP_'
end

.false?(variable) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/konfa.rb', line 98

def false?(variable)
  self.true?(variable) == false
end

.get(variable) ⇒ Object

The following methods provides the interface to this class



84
85
86
87
# File 'lib/konfa.rb', line 84

def get(variable)
  raise UnsupportedVariableError.new(variable) unless self.configuration.has_key? variable
  self.configuration[variable]
end

.get!(variable) ⇒ Object

Raises:



89
90
91
92
# File 'lib/konfa.rb', line 89

def get!(variable)
  raise NilVariableError.new(variable) if self.get(variable).nil?
  self.get(variable)
end

.initObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/konfa.rb', line 115

def init
  deprecated "[DEPRECATION] This style of initialization will no longer be supported in Konfa 1.0 and init "\
             "will be removed. Use initialize! or read_from/initialized! instead"
  return unless self.init?
  # Set to true before calling to prevent recursion if
  # an initializer is accessing the configuration
  self.initialized = true
  self.send(self.initializer.first, *self.initializer[1..-1])
  self.after_initialize
end

.init?Boolean

Returns:

  • (Boolean)


110
111
112
113
# File 'lib/konfa.rb', line 110

def init?
  deprecated "[DEPRECATION] init? will be removed in Konfa 1.0, use initialized? instead"
  !self.initialized && !self.initializer.nil?
end

.init_with(suffix, *args) ⇒ Object



126
127
128
129
130
# File 'lib/konfa.rb', line 126

def init_with(suffix, *args)
  deprecated "[DEPRECATION] init will be removed in Konfa 1.0. Use read_from instead"
  self.initializer = [:"init_with_#{suffix}", *args]
  self
end

.initialize!Object



145
146
147
148
149
150
151
# File 'lib/konfa.rb', line 145

def initialize!
  raise AlreadyInitializedError if self.initialized?

  @initialized = true
  self.after_initialize
  self
end

.initialized?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/konfa.rb', line 153

def initialized?
  @initialized == true
end

.on_key_missing(key, value) ⇒ Object



76
77
78
# File 'lib/konfa.rb', line 76

def on_key_missing(key, value)
  raise UnsupportedVariableError.new(key)
end

.read_from(initializer, *args) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/konfa.rb', line 137

def read_from(initializer, *args)
  raise AlreadyInitializedError if self.initialized?
  raise UnsupportedInitializerError unless self.respond_to?(:"init_with_#{initializer}")

  self.send(:"init_with_#{initializer}", *args)
  self
end

.reinitObject



132
133
134
135
# File 'lib/konfa.rb', line 132

def reinit
  deprecated "[DEPRECATION] reinit will be removed in Konfa 1.0. Use read_from to load multiple config files"
  self.initialized = false
end

.true?(variable) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/konfa.rb', line 94

def true?(variable)
  self.truthy?(self.get(variable))
end

.variablesObject



102
103
104
# File 'lib/konfa.rb', line 102

def variables
  self.configuration.keys
end

.with_config(overrides = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/konfa.rb', line 160

def with_config(overrides={})
  original_config = dump
  overrides.each_pair {|k,v| self.store(k, v) }

  begin
    result = yield
  ensure
    self.configuration = original_config
  end

  result
end