Created
December 20, 2011 04:43
-
-
Save mjc-gh/1500293 to your computer and use it in GitHub Desktop.
Settings Class
This file contains 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 Settings | |
attr_reader :config | |
def initialize(fname) | |
@file_name = fname | |
config = YAML.load_file(fname) rescue false | |
@config = config || {} | |
end | |
def flush! | |
File.open(@file_name, 'w+') do |file| | |
file.puts YAML.dump(@config) | |
file.tell | |
end | |
end | |
def reload! | |
initialize(@file_name) | |
end | |
def [] key | |
@config[key] | |
end | |
def []= key, val | |
@config[key] = val | |
end | |
def delete key | |
@config.delete key | |
end | |
def method_missing(method, value = nil, *args) | |
key = method.to_s | |
key.gsub!(/\=/, '') ? @config[key] = value : @config[key] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment