Module: Confix::InstanceMethods
- Included in:
- Config
- Defined in:
- lib/confix.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#get(key, default = nil) ⇒ Object
(also: #[])
Gets a setting by the given key.
-
#set(key, *value) ⇒ Object
(also: #[]=)
Sets a setting by the given key.
-
#to_hash ⇒ Object
Gets all current configuration values.
-
#update(hash) ⇒ Object
(Recursively) updates this configuration from a hash.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
269 270 271 |
# File 'lib/confix.rb', line 269 def method_missing(method, *args, &block) raise UndefinedSetting, "setting '#{self.class.(method)}' does not exist" end |
Class Method Details
.included(target) ⇒ Object
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/confix.rb', line 165 def self.included(target) # Delegate common hash functions to the hash. [ :each, :map, :select, :except, :symbolize_keys ].each do |method| target.class_eval <<-RUBY, __FILE__, __LINE__+1 def #{method}(*args, &block) to_hash.#{method} *args, &block end RUBY end end |
Instance Method Details
#get(key, default = nil) ⇒ Object Also known as: []
Gets a setting by the given key.
If the key refers to a child configuration, it retrieves an intermediate object that can be used for easy acess. See the examples in Confix.
If the key refers to an existing setting, its value is returned. If the value was not found or was nil, the given default value is returned.
If the value was not found, and no default was specified here, a default value for the setting is tried.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/confix.rb', line 189 def get(key, default = nil) key = key.to_s default = self.class.defaults[key] if default.nil? if self.class.configs[key] # If the key refers to a child configuration class, instantiate this. config_root.configs[self.class.(key)] ||= self.class.configs[key].new(self) elsif child? raise UndefinedSetting, "setting '#{self.class.(key)}' does not exist" unless self.class.key_defined?(key) # Ask the config_root object for the value. config_root.fetch(self.class.(key), default) else raise UndefinedSetting, "setting '#{self.class.(key)}' does not exist" unless self.class.key_defined?(key) fetch(key, default) end end |
#set(key, *value) ⇒ Object Also known as: []=
Sets a setting by the given key.
If the key refers to a child configuration, an exception is returned.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/confix.rb', line 212 def set(key, *value) if value.length == 0 && key.is_a?(Hash) # Apply the hash to the settings. key.each { |key, value| set key, value } else raise ArgumentError, 'too many arguments (1 or 2 expected)' if value.length > 1 value = value.first key = key.to_s if self.class.configs[key] raise CannotModifyConfiguration, "you cannot update a child configuration with anything else than a hash" unless value.is_a?(Hash) config = (config_root.configs[self.class.(key)] ||= self.class.configs[key].new(self)) config.update value elsif child? raise UndefinedSetting, "setting '#{self.class.(key)}' does not exist" unless self.class.key_defined?(key) config_root.values[self.class.(key)] = value else raise UndefinedSetting, "setting '#{self.class.(key)}' does not exist" unless self.class.key_defined?(key) values[key] = value end end end |
#to_hash ⇒ Object
Gets all current configuration values.
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/confix.rb', line 238 def to_hash values = {} self.class.settings.each do |key| values[key] = get(key) end self.class.configs.each do |key, config| values[key] = get(key).to_hash end values end |
#update(hash) ⇒ Object
(Recursively) updates this configuration from a hash.
250 251 252 253 254 255 256 257 258 259 |
# File 'lib/confix.rb', line 250 def update(hash) if hash hash.each do |key, value| set key, value end end self end |