Created
June 10, 2014 21:07
-
-
Save scalone/225f86339e3594b59913 to your computer and use it in GitHub Desktop.
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 FileDb | |
def self.open(path) | |
if File.exist?(path) | |
file = File.open(path, "r") | |
self.parse(file.read) | |
else | |
Hash.new | |
end | |
ensure | |
file.close if file | |
end | |
def self.parse(text) | |
params = Hash.new | |
text.each_line do |line| | |
key_value = line.split("=") | |
params[key_value[0]] = key_value[1] | |
end | |
params | |
end | |
# TODO Scalone: think in atomic operation | |
# Rename in pax create a new file, so isn't a SO/System file operation | |
def self.persist(path, params) | |
file_new = File.open(path, "w+") | |
params.each do |line_key, line_value| | |
file_new.write("#{line_key}=#{line_value}\n") | |
end | |
file_new.close | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment