Class: Bbiff::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/bbiff/settings.rb

Constant Summary collapse

APP_NAME =
'bbiff'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSettings

Returns a new instance of Settings.



11
12
13
14
15
# File 'lib/bbiff/settings.rb', line 11

def initialize
  @current = default.dup
  @config_dir = "#{ ENV['HOME'] }/.config/#{ APP_NAME }"
  load
end

Instance Attribute Details

#currentObject

Returns the value of attribute current.



7
8
9
# File 'lib/bbiff/settings.rb', line 7

def current
  @current
end

Instance Method Details

#defaultObject



17
18
19
# File 'lib/bbiff/settings.rb', line 17

def default
  { 'delay_seconds' => 10, 'bbiff_show' => 'bbiff-show' }
end

#loadObject



21
22
23
24
25
26
# File 'lib/bbiff/settings.rb', line 21

def load
  if File.readable?("#{@config_dir}/settings.tml")
    prefs = TOML.load_file("#{@config_dir}/settings.tml")
    self.current = current.merge(prefs)
  end
end

#saveObject



28
29
30
31
32
33
34
# File 'lib/bbiff/settings.rb', line 28

def save
  FileUtils.mkdir_p(@config_dir)
  prefs = (current.to_a - default.to_a).to_h
  File.open("#{@config_dir}/settings.tml", 'w') do |f|
    f.write(TOML.dump(prefs))
  end
end