Class: Fuel::Util::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/fuel/util/config.rb

Constant Summary collapse

CONFIG_PATH =
File.expand_path("~/.fuel")
CONFIG_FILENAME =
"#{CONFIG_PATH}/cliconfig"

Class Method Summary collapse

Class Method Details

.add(key, value) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/fuel/util/config.rb', line 29

def self.add(key, value)
  ensure_config_exists

  config = load
  config[key] ||= []
  config[key] << value
  save(config)
end

.ensure_config_existsObject



48
49
50
51
# File 'lib/fuel/util/config.rb', line 48

def self.ensure_config_exists
  FileUtils.mkdir_p(CONFIG_PATH)
  FileUtils.touch(CONFIG_FILENAME)
end

.get(*keys) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/fuel/util/config.rb', line 20

def self.get(*keys)
  ensure_config_exists

  config = load
  keys.inject(config, :fetch)
rescue KeyError
  nil
end

.loadObject



40
41
42
# File 'lib/fuel/util/config.rb', line 40

def self.load
  YAML.load_file(CONFIG_FILENAME) || {}
end

.save(config) ⇒ Object



44
45
46
# File 'lib/fuel/util/config.rb', line 44

def self.save(config)
  File.open(CONFIG_FILENAME, "w") { |f| f.write(YAML.dump(config)) }
end

.set(*keys, value) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/fuel/util/config.rb', line 10

def self.set(*keys, value)
  ensure_config_exists

  config = load
  key = keys.pop
  h = keys.inject(config) { |memo, k| memo[k] ||= {}; memo[k] }
  h[key] = value
  save(config)
end