Skip to content

Instantly share code, notes, and snippets.

@brettbuddin
Created February 16, 2011 20:34
Show Gist options
  • Save brettbuddin/830134 to your computer and use it in GitHub Desktop.
Save brettbuddin/830134 to your computer and use it in GitHub Desktop.
Small config manager.
class Config
def initialize
@config = {}
end
def load_file(file_name)
instance_eval(File.read(file_name))
self
end
def set(key, value)
@config[key.to_sym] = value
end
def get(key)
@config[key]
end
def delete(key)
@config.delete key.to_sym
end
def exists?(key)
@config.has_key? key
end
private
def method_missing(sym, *args)
if args.length == 0 && @config.has_key?(sym)
get(sym)
end
end
end
config = Config.new
config.load_file File.join(File.dirname(__FILE__), "sample.rb")
config.get(:foo) #=> "bar"
config.get(:save_path) #=> "/var/www/download"
set :foo, "bar"
set :web_path, "/var/www"
set :save_path, File.join(web_path, "download")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment