module Rrutils::Confdb

This modul represents a mixin for configuration that can be ‘loaded from’ or ‘stored into’ a persistent repository.

Public Instance Methods

read(input_stream=STDIN) click to toggle source

Loads and returns a configuration from a file.

# File lib/rrutils/confdb.rb, line 12
def read(input_stream=STDIN)
  rslt = {}
  unless input_stream.nil?
    begin
      rslt = JSON.load(input_stream) # returns 'nil' if empty file
      rslt ||= {}
      Dumon::logger.debug "Configuration readed, keys=#{rslt.keys}"
    rescue => e
      Dumon::logger.warn "failed to read configuration: #{e.message}"
    ensure
      input_stream.close unless input_stream === STDIN
    end
  end

  rslt
end
write(conf, output_stream=STDOUT) click to toggle source

Writes out the configuration to a given output stream.

# File lib/rrutils/confdb.rb, line 31
def write(conf, output_stream=STDOUT)
  raise ArgumentError, 'configuration not a hash' unless conf.is_a? Hash

  begin
    output_stream.write(JSON.pretty_generate(conf))
    Dumon::logger.debug "Configuration written, keys=#{conf.keys}"
  rescue => e
    Dumon::logger.error "failed to write configuration: #{e.message}"
  ensure
    output_stream.close unless output_stream === STDOUT
  end
end