Last active
December 10, 2015 19:39
-
-
Save clicube/4482749 to your computer and use it in GitHub Desktop.
SimpleStore is initialized with file path of YAML file, can be used like Hash, and can be saved to the file.
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
# coding: utf-8 | |
require 'yaml' | |
class SimpleStore < BasicObject | |
attr_reader :file | |
def initialize(file) | |
@file = file | |
if(::File::exist?(file)) | |
@data ||= ::YAML::load(open(file)) | |
else | |
@data = {} | |
end | |
end | |
def save | |
::File.open(file, 'w') { |f| f.write(::YAML.dump(@data)) } | |
self | |
end | |
def method_missing(*args, &block) | |
@data.__send__(*args,&block) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment