Class: Pushover::ConfigBlob

Inherits:
Hash
  • Object
show all
Defined in:
lib/pushover/config.rb

Overview

This is an extended [Hash] that adds some saving features via yajl.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(load = true) ⇒ ConfigBlob

Returns a new instance of ConfigBlob.



13
14
15
16
17
# File 'lib/pushover/config.rb', line 13

def initialize(load = true)
  @save_file = "#{Dir.home}/.config/pushover/config.json"

  self.load if load
end

Instance Attribute Details

#save_fileObject

Returns the value of attribute save_file.



6
7
8
# File 'lib/pushover/config.rb', line 6

def save_file
  @save_file
end

Instance Method Details

#backupSaveObject

Backup our save file, will remove original in the process.



36
37
38
# File 'lib/pushover/config.rb', line 36

def backupSave
  FileUtils.mv @save_file, "#{@save_file}.bak" if File.file? @save_file
end

#clearObject

Clear the config file (not implemented)



20
21
# File 'lib/pushover/config.rb', line 20

def clear
end

#loadObject

Load the config file if it is available.



52
53
54
55
56
57
# File 'lib/pushover/config.rb', line 52

def load
  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

#saveObject

Save the config, will raise an exception if the file exists.



24
25
26
27
28
29
30
31
32
33
# File 'lib/pushover/config.rb', line 24

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(@save_file, 'w')
    f.write Yajl.dump self
    f.sync
    f.close
  end
end

#save!(backup = true) ⇒ Object

Save the config, removing the existing one if necessary.



41
42
43
44
45
46
47
48
49
# File 'lib/pushover/config.rb', line 41

def save!(backup = true)
  if backup
    backupSave
  else
    FileUtils.rm @save_file if File.file? @save_file
  end

  save
end

#save_dirString

Returns the dirname of the save file.

Returns:

  • (String)

    the dirname of the save file.



9
10
11
# File 'lib/pushover/config.rb', line 9

def save_dir
  File.dirname @save_file
end