Skip to content

Instantly share code, notes, and snippets.

@jsonperl
Created May 22, 2015 22:23
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 jsonperl/db25e0256e139aa66127 to your computer and use it in GitHub Desktop.
Save jsonperl/db25e0256e139aa66127 to your computer and use it in GitHub Desktop.
CheapDB
class CheapDB
def initialize(filename)
@file = "#{filename}.yml"
FileUtils.touch @file
@data = YAML.load_file(@file) || {}
end
def get_or_set(key, &block)
get(key) || set(key, block.call)
end
def get(key)
@data[key]
end
def set(key, value)
@data[key] = value
File.open(@file, 'w') { |f| f.write @data.to_yaml }
value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment