Class: Kshramt::Config

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/kshramt/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(app_name, default = {}) ⇒ Config

Returns a new instance of Config.


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/kshramt/config.rb', line 7

def initialize(app_name, default = {})
  @file = File.join(ENV['HOME'], '.config', app_name, 'config.yaml')
  loaded_config = if File.readable?(@file) && File.size(@file) > 0
                    YAML.load_file(@file)
                  else
                    {}
                  end
  config = default.merge(loaded_config)
  @table = config
  if config.size > loaded_config.size
    self.dump_file(@file, default)
  end
end

Instance Method Details

#dump(additional_config = {}) ⇒ Object


29
30
31
# File 'lib/kshramt/config.rb', line 29

def dump(additional_config = {})
  YAML.dump(self.to_h.merge(additional_config))
end

#dump_file(file = @file, additional_config = {}) ⇒ Object


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

def dump_file(file = @file, additional_config = {})
  FileUtils.mkdir_p(File.dirname(file))
  open(file, 'w') do |io|
    io.write(self.dump(additional_config))
    io.flush
  end
end