#encoding: UTF-8 require 'yaml' class Redis module Stream # Simple way to read and manage a config.yml file class Config @config = {} @config_file_path = "" # return the current location of the config.yml file # @return [String] path of config.yml def self.path @config_file_path end # set path to config file # @param [String] config_file_path path to config.yml file def self.path=(config_file_path) @config_file_path = config_file_path end # get the value for a key # @param [String] key key of key/value # @return [Object] value of key/value pair def self.[](key) init @config[key] end # set a value into the config.yml file # @param [String] key # @param [Object] value # @return [Object] def self.[]=(key, value) init @config[key] = value File.open("#{path}/config.yml", 'w') do |f| f.puts @config.to_yaml end end #is key available in config store # @param [String] key key to lookup # @return [Boolean] def self.include?(key) init @config.include?(key) end private # load and prepare config.yml def self.init discover_config_file_path if @config.empty? config = YAML::load_file("#{path}/config.yml") @config = process(config) end end private #determine location of config.yml file def self.discover_config_file_path if @config_file_path.nil? || @config_file_path.empty? if File.exist?('config.yml') @config_file_path = '.' elsif File.exist?("config/config.yml") @config_file_path = 'config' end end end #process config.yml file # @param [Object] config yaml data # @return [Object] def self.process(config) new_config = {} config.each do |k, v| if config[k].is_a?(Hash) v = process(v) end new_config.store(k.to_sym, v) end new_config end end end end