Module: Rrutils::Confdb
- Included in:
- Dumon::App
- Defined in:
- lib/rrutils/confdb.rb
Overview
This modul represents a mixin for configuration that can be ‘loaded from’ or ‘stored into’ a persistent repository.
Instance Method Summary collapse
-
#read(input_stream = STDIN) ⇒ Object
Loads and returns a configuration from a file.
-
#write(conf, output_stream = STDOUT) ⇒ Object
Writes out the configuration to a given output stream.
Instance Method Details
#read(input_stream = STDIN) ⇒ Object
Loads and returns a configuration from a file.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# 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.}" ensure input_stream.close unless input_stream === STDIN end end rslt end |
#write(conf, output_stream = STDOUT) ⇒ Object
Writes out the configuration to a given output stream.
31 32 33 34 35 36 37 38 39 40 41 42 |
# 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.}" ensure output_stream.close unless output_stream === STDOUT end end |