Skip to content

Instantly share code, notes, and snippets.

@mjc-gh
Created December 20, 2011 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjc-gh/1500293 to your computer and use it in GitHub Desktop.
Save mjc-gh/1500293 to your computer and use it in GitHub Desktop.
Settings Class
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