module Confix::InstanceMethods

Public Class Methods

included(target) click to toggle source
# 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

Public Instance Methods

[](key, default = nil)
Alias for: get
[]=(key, *value)
Alias for: set
get(key, default = nil) click to toggle source

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.

# 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.expand_key(key)] ||= self.class.configs[key].new(self)
  elsif child?
    raise UndefinedSetting, "setting '#{self.class.expand_key(key)}' does not exist" unless self.class.key_defined?(key)

    # Ask the config_root object for the value.
    config_root.fetch(self.class.expand_key(key), default)
  else
    raise UndefinedSetting, "setting '#{self.class.expand_key(key)}' does not exist" unless self.class.key_defined?(key)

    fetch(key, default)
  end
end
Also aliased as: []
set(key, *value) click to toggle source

Sets a setting by the given key.

If the key refers to a child configuration, an exception is returned.

# 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.expand_key(key)] ||= self.class.configs[key].new(self))
      config.update value
    elsif child?
      raise UndefinedSetting, "setting '#{self.class.expand_key(key)}' does not exist" unless self.class.key_defined?(key)
      config_root.values[self.class.expand_key(key)] = value
    else
      raise UndefinedSetting, "setting '#{self.class.expand_key(key)}' does not exist" unless self.class.key_defined?(key)
      values[key] = value
    end
  end
end
Also aliased as: []=
to_hash() click to toggle source

Gets all current configuration values.

# 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) click to toggle source

(Recursively) updates this configuration from a hash.

# File lib/confix.rb, line 250
def update(hash)

  if hash
    hash.each do |key, value|
      set key, value
    end
  end

  self
end

Private Instance Methods

method_missing(method, *args, &block) click to toggle source

Method missing

# File lib/confix.rb, line 269
def method_missing(method, *args, &block)
  raise UndefinedSetting, "setting '#{self.class.expand_key(method)}' does not exist"
end