Module: Confix::ClassMethods
- Included in:
- Config
- Defined in:
- lib/confix.rb
Instance Attribute Summary collapse
-
#key_from_root ⇒ Object
readonly
Returns the value of attribute key_from_root.
Instance Method Summary collapse
-
#config(key, template = nil, &block) ⇒ Object
Defines a child configuration for this configuration.
- #config_root ⇒ Object
- #configs ⇒ Object
- #defaults ⇒ Object
-
#expand_key(key) ⇒ Object
Support.
- #key_defined?(key) ⇒ Boolean
-
#setting(key, default = nil) ⇒ Object
Defines a setting for this configuration.
-
#settings ⇒ Object
Attributes.
Instance Attribute Details
#key_from_root ⇒ Object (readonly)
Returns the value of attribute key_from_root.
341 342 343 |
# File 'lib/confix.rb', line 341 def key_from_root @key_from_root end |
Instance Method Details
#config(key, template = nil, &block) ⇒ Object
Defines a child configuration for this configuration.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/confix.rb', line 295 def config(key, template = nil, &block) key = key.to_s raise ArgumentError, "invalid key: #{key}" unless Confix.valid_key?(key) config = Class.new(Config) config.instance_variable_set '@parent', self config.instance_variable_set '@key_from_root', (key) # If no template or block are specified, infer a default template from the name. if !template && !block template = key raise ArgumentError, "no template or block specified, and no template :#{key} found" unless config_root.templates[template] elsif template template = template.to_s raise ArgumentError, "template :#{key} not found" unless config_root.templates[template] end # If a template is specified, first apply its block. config.class_eval &config_root.templates[template] if template # Apply the block. config.class_eval &block if block # Wrap up. define_accessor_methods key configs[key] = config config end |
#config_root ⇒ Object
337 338 339 |
# File 'lib/confix.rb', line 337 def config_root @parent ? @parent.config_root : self end |
#configs ⇒ Object
333 334 335 |
# File 'lib/confix.rb', line 333 def configs @configs ||= {} end |
#defaults ⇒ Object
330 331 332 |
# File 'lib/confix.rb', line 330 def defaults @defaults ||= {} end |
#expand_key(key) ⇒ Object
Support
346 347 348 |
# File 'lib/confix.rb', line 346 def (key) [ @key_from_root, key ].compact.join('.') end |
#key_defined?(key) ⇒ Boolean
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/confix.rb', line 350 def key_defined?(key) if key.is_a?(Array) tail = key else tail = key.to_s.split('.') end head = tail.shift if configs.keys.include?(head) configs[head].key_defined?(tail) elsif settings.include?(head) && tail.empty? true else false end end |
#setting(key, default = nil) ⇒ Object
Defines a setting for this configuration. If this configuration was defined as a child of some parent configuration, this parent configuration will also create a definition for this setting, but no accessor methods.
285 286 287 288 289 290 291 292 |
# File 'lib/confix.rb', line 285 def setting(key, default = nil) key = key.to_s raise ArgumentError, "invalid key: #{key}" unless Confix.valid_key?(key) settings << key defaults[key] = default if default define_accessor_methods key end |
#settings ⇒ Object
Attributes
327 328 329 |
# File 'lib/confix.rb', line 327 def settings @settings ||= [] end |