Module: Ukey::Config

Defined in:
lib/ukey/config.rb

Overview

reads and writes the configuration for ukey

Class Method Summary collapse

Class Method Details

.config_directoryObject



45
46
47
# File 'lib/ukey/config.rb', line 45

def config_directory
  File.expand_path('~/.config/ukey')
end

.config_pathObject



41
42
43
# File 'lib/ukey/config.rb', line 41

def config_path
  File.join(config_directory, 'ukey.yml')
end

.deviceObject



23
24
25
# File 'lib/ukey/config.rb', line 23

def device
  load_config[:device] || raise(DeviceNotSetError)
end

.device=(device_name) ⇒ Object



18
19
20
21
# File 'lib/ukey/config.rb', line 18

def device=(device_name)
  config = load_config.merge(device: device_name)
  write_config(config)
end

.intervalObject



32
33
34
# File 'lib/ukey/config.rb', line 32

def interval
  (load_config[:interval] || 5).to_i
end

.interval=(interval) ⇒ Object



27
28
29
30
# File 'lib/ukey/config.rb', line 27

def interval=(interval)
  config = load_config.merge(interval: interval.to_i)
  write_config(config)
end

.load_configObject



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

def load_config
  return {} unless File.exist?(config_path)
  YAML.load_file(config_path) || {}
end

.write_config(config) ⇒ Object



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

def write_config(config)
  FileUtils.mkdir_p(config_directory)
  File.open(config_path, 'w') { |f| YAML.dump(config, f) }
end