Created
February 16, 2011 20:34
-
-
Save brettbuddin/830134 to your computer and use it in GitHub Desktop.
Small config manager.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| config = Config.new | |
| config.load_file File.join(File.dirname(__FILE__), "sample.rb") | |
| config.get(:foo) #=> "bar" | |
| config.get(:save_path) #=> "/var/www/download" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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