Class: Uffizzi::ConfigFile

Inherits:
Object
  • Object
show all
Defined in:
lib/uffizzi/config_file.rb

Constant Summary collapse

CONFIG_DIR =
"#{Dir.home}/.config/uffizzi"
CONFIG_PATH =
"#{Dir.home}/.config/uffizzi/config_default.json"

Class Method Summary collapse

Class Method Details

.config_pathObject



12
13
14
# File 'lib/uffizzi/config_file.rb', line 12

def config_path
  CONFIG_PATH
end

.deleteObject



16
17
18
# File 'lib/uffizzi/config_file.rb', line 16

def delete
  File.truncate(config_path, 0) if exists?
end

.exists?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/uffizzi/config_file.rb', line 20

def exists?
  File.exist?(config_path)
end

.listObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/uffizzi/config_file.rb', line 60

def list
  data = read

  content = data.reduce('') do |acc, pair|
    property, value = pair
    "#{acc}#{property} = #{value}\n"
  end

  Uffizzi.ui.say(content)

  data
end

.option_exists?(option) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/uffizzi/config_file.rb', line 73

def option_exists?(option)
  data = read

  data.key?(option)
end

.option_has_value?(option) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/uffizzi/config_file.rb', line 34

def option_has_value?(option)
  data = read
  return false unless option_exists?(option)

  data[option].present?
end

.read_option(option, nested_option = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/uffizzi/config_file.rb', line 24

def read_option(option, nested_option = nil)
  data = read

  value = data[option]
  return value.presence if nested_option.nil?
  return nil unless value.is_a?(Hash)

  value[nested_option].presence
end


56
57
58
# File 'lib/uffizzi/config_file.rb', line 56

def rewrite_cookie(cookie)
  write_option(:cookie, cookie)
end

.unset_option(key) ⇒ Object



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

def unset_option(key)
  data = read
  return unless option_exists?(key)

  data[key] = ''
  write(data)
end

.write_option(key, value) ⇒ Object



41
42
43
44
45
46
# File 'lib/uffizzi/config_file.rb', line 41

def write_option(key, value)
  data = exists? ? read : {}

  data[key] = value
  write(data)
end