Class: Konfa::Base
- Inherits:
-
Object
- Object
- Konfa::Base
- Includes:
- Deprecation, Initializer
- Defined in:
- lib/konfa.rb
Class Method Summary collapse
- .after_initialize ⇒ Object
- .allowed_variables ⇒ Object
- .dump ⇒ Object
-
.env_variable_prefix ⇒ Object
The following methods should be overridden and used to configure the configuration class.
- .false?(variable) ⇒ Boolean
-
.get(variable) ⇒ Object
The following methods provides the interface to this class.
- .get!(variable) ⇒ Object
- .init ⇒ Object
- .init? ⇒ Boolean
- .init_with(suffix, *args) ⇒ Object
- .initialize! ⇒ Object
- .initialized? ⇒ Boolean
- .on_key_missing(key, value) ⇒ Object
- .read_from(initializer, *args) ⇒ Object
- .reinit ⇒ Object
- .true?(variable) ⇒ Boolean
- .variables ⇒ Object
- .with_config(overrides = {}) ⇒ Object
Methods included from Deprecation
Methods included from Initializer
Class Method Details
.after_initialize ⇒ Object
158 159 |
# File 'lib/konfa.rb', line 158 def after_initialize end |
.allowed_variables ⇒ Object
73 74 75 |
# File 'lib/konfa.rb', line 73 def allowed_variables {} end |
.dump ⇒ Object
107 108 109 |
# File 'lib/konfa.rb', line 107 def dump self.configuration.dup end |
.env_variable_prefix ⇒ Object
The following methods should be overridden and used to configure the configuration class
69 70 71 |
# File 'lib/konfa.rb', line 69 def env_variable_prefix 'APP_' end |
.false?(variable) ⇒ Boolean
99 100 101 |
# File 'lib/konfa.rb', line 99 def false?(variable) self.true?(variable) == false end |
.get(variable) ⇒ Object
The following methods provides the interface to this class
85 86 87 88 |
# File 'lib/konfa.rb', line 85 def get(variable) raise UnsupportedVariableError.new(variable) unless self.configuration.has_key? variable self.configuration[variable] end |
.get!(variable) ⇒ Object
90 91 92 93 |
# File 'lib/konfa.rb', line 90 def get!(variable) raise NilVariableError.new(variable) if self.get(variable).nil? self.get(variable) end |
.init ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/konfa.rb', line 116 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
111 112 113 114 |
# File 'lib/konfa.rb', line 111 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
127 128 129 130 131 |
# File 'lib/konfa.rb', line 127 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
146 147 148 149 150 151 152 |
# File 'lib/konfa.rb', line 146 def initialize! raise AlreadyInitializedError if self.initialized? @initialized = true self.after_initialize self end |
.initialized? ⇒ Boolean
154 155 156 |
# File 'lib/konfa.rb', line 154 def initialized? @initialized == true end |
.on_key_missing(key, value) ⇒ Object
77 78 79 |
# File 'lib/konfa.rb', line 77 def on_key_missing(key, value) raise UnsupportedVariableError.new(key) end |
.read_from(initializer, *args) ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/konfa.rb', line 138 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 |
.reinit ⇒ Object
133 134 135 136 |
# File 'lib/konfa.rb', line 133 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
95 96 97 |
# File 'lib/konfa.rb', line 95 def true?(variable) self.truthy?(self.get(variable)) end |
.variables ⇒ Object
103 104 105 |
# File 'lib/konfa.rb', line 103 def variables self.configuration.keys end |
.with_config(overrides = {}) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/konfa.rb', line 161 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 |