Module: Confix::ClassMethods

Included in:
Config
Defined in:
lib/confix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#key_from_rootObject (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.

Raises:

  • (ArgumentError)


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', expand_key(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_rootObject



337
338
339
# File 'lib/confix.rb', line 337

def config_root
  @parent ? @parent.config_root : self
end

#configsObject



333
334
335
# File 'lib/confix.rb', line 333

def configs
  @configs ||= {}
end

#defaultsObject



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 expand_key(key)
  [ @key_from_root, key ].compact.join('.')
end

#key_defined?(key) ⇒ Boolean

Returns:

  • (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.

Parameters:

  • default (Object) (defaults to: nil)

    Specify a default value for the setting.

Raises:

  • (ArgumentError)


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

#settingsObject

Attributes



327
328
329
# File 'lib/confix.rb', line 327

def settings
  @settings ||= []
end