require 'yaml' class Configuration include Enumerable @@config_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'etc')) def initialize(name) @config_name = name end def config_file File.join(@@config_path, @config_name) end def load_config STDERR.puts "Reloading configuration" @config_hash = indifferent_params(YAML::load(File.open(config_file))) @config_time = File.stat(config_file).mtime end def stale? File.stat(config_file).mtime != @config_time end def config_hash load_config if !@config_hash || stale? @config_hash end def each(*args, &block) config_hash.each(*args, &block) end def [](key) config_hash[key] end def indifferent_hash Hash.new {|hash,key| hash[key.to_s] if Symbol === key } end def indifferent_params(object) case object when Hash new_hash = indifferent_hash object.each {|key, value| new_hash[key] = indifferent_params(value) } new_hash when Array object.map {|item| indifferent_params(item) } else object end end def self.load(name) new(name) end end