lib/pushover/config.rb in pushover-0.2.0 vs lib/pushover/config.rb in pushover-0.3.1
- old
+ new
@@ -1,40 +1,62 @@
require 'fileutils'
module Pushover
+ # This is an extended [Hash] that adds some saving features via yajl.
class ConfigBlob < Hash
- BaseDir = "#{Dir.home}/.config/pushover"
+ attr_accessor :save_file
+ # @return [String] the dirname of the save file.
+ def save_dir
+ File.dirname @save_file
+ end
+
def initialize(load = true)
- FileUtils.mkdir_p BaseDir if !Dir.exist? BaseDir
+ @save_file = "#{Dir.home}/.config/pushover/config.json"
+
self.load if load
end
- def file
- "#{BaseDir}/config.json"
+ # Clear the config file (not implemented)
+ def clear
end
+ # Save the config, will raise an exception if the file exists.
def save
+ FileUtils.mkdir_p save_dir if !Dir.exist? save_dir
if any?
# I do this the long way because I want an immediate sync.
- f = open(file, 'w')
+ f = open(@save_file, 'w')
f.write Yajl.dump self
f.sync
f.close
end
end
- def save!
- FileUtils.rm file if File.file? file
+ # Backup our save file, will remove original in the process.
+ def backupSave
+ FileUtils.mv @save_file, "#{@save_file}.bak" if File.file? @save_file
+ end
+
+ # Save the config, removing the existing one if necessary.
+ def save!(backup = true)
+ if backup
+ backupSave
+ else
+ FileUtils.rm @save_file if File.file? @save_file
+ end
+
save
end
+ # Load the config file if it is available.
def load
- if File.exist?(self.file) && File.stat(self.file).size > 0
- h = Yajl.load open(file, 'r').read
+ if File.exist?(@save_file) && File.stat(@save_file).size > 0
+ h = Yajl.load open(@save_file, 'r').read
h.each { |k,v| self[k.to_sym] = v}
end
end
end
+ # A convenience instance of config, provides Pushover.Config.
Config = ConfigBlob.new
end