class Qaa::Configuration

Public Class Methods

config() click to toggle source
# File lib/qaa/configuration.rb, line 7
def self.config
  @config ||= {}
end
fetch(key, default='') click to toggle source
# File lib/qaa/configuration.rb, line 21
def self.fetch (key, default='')
  if self.has_key? key
    self.[] key
  else
    default
  end
end
load(profile_name, file_path, reset=false) click to toggle source
# File lib/qaa/configuration.rb, line 11
def self.load(profile_name, file_path, reset=false)
  begin
    new_config = YAML.load(ERB.new(File.read(file_path)).result)[profile_name]
    @config = {} if reset
    config_merge!(new_config)
  rescue Exception => e
    raise "Could not locate a configuration named \"#{profile_name}\" in \"#{file_path}\", #{e.message}"
  end
end

Private Class Methods

[](key) click to toggle source
# File lib/qaa/configuration.rb, line 54
def self.[] (key)
  if config.has_key? key
    config[key]
  else
    cur = config
    key.split('.').each do |key_part|
      if cur.has_key? key_part
        cur = cur[key_part]
      else
        return nil
      end
    end
    cur
  end
end
[]=(key, value) click to toggle source
# File lib/qaa/configuration.rb, line 42
def self.[]= (key, value)
  cur       = config
  key_parts = key.split('.')
  key_parts[0..-2].each do |key_part|
    if (!cur.has_key? key_part) || cur[key_part].nil?
      cur[key_part] = {}
    end
    cur = cur[key_part]
  end
  cur[key_parts.last] = value
end
config_merge!(config_hash) click to toggle source
# File lib/qaa/configuration.rb, line 87
def self.config_merge! (config_hash)
  config_hash.each_pair do |k, v|
    self.deep_merge! k, v
  end
end
deep_merge!(key, value) click to toggle source
# File lib/qaa/configuration.rb, line 31
def self.deep_merge! (key, value)
  if value.is_a?(Hash)
    value.each_pair do |k, v|
      self.deep_merge! "#{key}.#{k}", v
    end
  else
    self.[]= key, value
  end
end
has_key?(key) click to toggle source
# File lib/qaa/configuration.rb, line 70
def self.has_key? (key)
  if config.has_key? key
    true
  else
    cur = config
    key.split('.').each do |key_part|
      if cur && (cur.has_key? key_part)
        cur = cur[key_part]
      else
        return false
      end
    end
    return true
  end
end