Class: CLI::Kit::Config

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/cli/kit/config.rb

Constant Summary collapse

XDG_CONFIG_HOME =
'XDG_CONFIG_HOME'

Instance Method Summary collapse

Methods included from T::Sig

sig

Constructor Details

#initialize(tool_name:) ⇒ Config

Returns a new instance of Config.



14
15
16
# File 'lib/cli/kit/config.rb', line 14

def initialize(tool_name:)
  @tool_name = tool_name
end

Instance Method Details

#fileObject



111
112
113
114
# File 'lib/cli/kit/config.rb', line 111

def file
  config_home = ENV.fetch(XDG_CONFIG_HOME, '~/.config')
  File.expand_path(File.join(@tool_name, 'config'), config_home)
end

#get(section, name, default: nil) ⇒ Object



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

def get(section, name, default: nil)
  all_configs.dig("[#{section}]", name) || default
end

#get_bool(section, name, default: false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cli/kit/config.rb', line 38

def get_bool(section, name, default: false)
  case get(section, name)
  when 'true'
    true
  when 'false'
    false
  when nil
    default
  else
    raise CLI::Kit::Abort, "Invalid config: #{section}.#{name} is expected to be true or false"
  end
end

#get_section(section) ⇒ Object



96
97
98
# File 'lib/cli/kit/config.rb', line 96

def get_section(section)
  (all_configs["[#{section}]"] || {}).dup
end

#set(section, name, value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/cli/kit/config.rb', line 62

def set(section, name, value)
  all_configs["[#{section}]"] ||= {}
  case value
  when nil
    T.must(all_configs["[#{section}]"]).delete(name)
  else
    T.must(all_configs["[#{section}]"])[name] = value.to_s
  end
  write_config
end

#to_sObject



101
102
103
# File 'lib/cli/kit/config.rb', line 101

def to_s
  ini.to_s
end

#unset(section, name) ⇒ Object



83
84
85
# File 'lib/cli/kit/config.rb', line 83

def unset(section, name)
  set(section, name, nil)
end