Created
June 6, 2014 00:35
-
-
Save fkaa/a7a61daee69e64d17301 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
| module Kaffeh | |
| class Config | |
| def initialize(cfg) | |
| self.path = cfg | |
| load | |
| end | |
| def path=(path) | |
| if File.exists?(path) and File.file?(path) | |
| @path = path | |
| elsif not File.exists?(path) | |
| File.open(path, "w") {} | |
| @path = path | |
| end | |
| end | |
| def path | |
| return @path | |
| end | |
| def load | |
| lines = File.open(@path, "r") do |f| | |
| f.read() | |
| end | |
| @settings = JSON.parse(lines) | |
| end | |
| def save | |
| File.open(@path, "w") do |file| | |
| file.puts JSON.generate(@settings, {:pretty_print => true}) | |
| end | |
| end | |
| def []=(key, val) | |
| put(key, val) | |
| end | |
| def put(key, val) | |
| @settings[key] = val | |
| end | |
| def [](key) | |
| return get(key) | |
| end | |
| def get(key) | |
| return @settings[key] | |
| end | |
| def get?(key, fallback) | |
| if @settings.key?(key) | |
| return @settings[key] | |
| elsif fallback != nil | |
| put(key, fallback) | |
| return fallback | |
| else | |
| return nil | |
| end | |
| end | |
| def to_s | |
| return @settings.to_s | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment