Module: Settings

Defined in:
lib/droxi/settings.rb

Overview

Manages persistent (session-independent) application state.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.config_file_pathObject

The path of the application’s rc file.



5
6
7
# File 'lib/droxi/settings.rb', line 5

def config_file_path
  @config_file_path
end

.dirtyObject

true if the settings have been modified since last write, false otherwise.



61
62
63
# File 'lib/droxi/settings.rb', line 61

def dirty
  @dirty
end

.settingsObject

A Hash of setting keys to values.



64
65
66
# File 'lib/droxi/settings.rb', line 64

def settings
  @settings
end

Class Method Details

.[](key) ⇒ Object

Return the value of a setting, or nil if the setting does not exist.



11
12
13
# File 'lib/droxi/settings.rb', line 11

def self.[](key)
  settings[key]
end

.[]=(key, value) ⇒ Object

Set the value of a setting.



16
17
18
19
20
# File 'lib/droxi/settings.rb', line 16

def self.[]=(key, value)
  return value if value == settings[key]
  self.dirty = true
  settings[key] = value
end

.delete(key) ⇒ Object

Delete the setting and return its value.



28
29
30
31
32
# File 'lib/droxi/settings.rb', line 28

def self.delete(key)
  return unless settings.include?(key)
  self.dirty = true
  settings.delete(key)
end

.include?(key) ⇒ Boolean

Return true if the setting exists, false otherwise.

Returns:

  • (Boolean)


23
24
25
# File 'lib/droxi/settings.rb', line 23

def self.include?(key)
  settings.include?(key)
end

.readObject

Read and parse the rc file.



48
49
50
51
52
53
54
# File 'lib/droxi/settings.rb', line 48

def self.read
  self.dirty = false
  return {} unless File.exist?(config_file_path)
  File.open(config_file_path) do |file|
    file.each_line.reduce({}) { |a, e| a.merge(parse(e.strip)) }
  end
end

.saveObject

Write settings to disk.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/droxi/settings.rb', line 35

def self.save
  return unless dirty
  self.dirty = false
  require 'fileutils'
  FileUtils.mkdir_p(File.dirname(config_file_path))
  File.open(config_file_path, 'w') do |file|
    settings.each_pair { |k, v| file.write("#{k}=#{v}\n") }
    file.chmod(0600)
  end
  nil
end