Class: App::Config
Overview
the priority of configuation settings is:
1. command line options
2. configuration from custom file, eg.: --config=/tmp/foo.yml
3. built-in default values from yaml file next to $0 script
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#defaults_path ⇒ Object
readonly
Returns the value of attribute defaults_path.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ Config
constructor
params are :argv and :defaults_path which are defaulting to ARGV and the applications defaults which are loaded from a yaml file next to the ‘$0’ application script.
-
#load_config_file(cpath) ⇒ Object
overwrite current configuration with values from file.
-
#set_default_values(defaults) ⇒ Object
programatic way to define the default values other then from defaults file.
- #to_s ⇒ Object
Methods included from DL::LoggerMixin
#debug, #error, #fatal, #info, #log_level, #logger, #set_log_level, #warn
Constructor Details
#initialize(params = {}) ⇒ Config
params are :argv and :defaults_path which are defaulting to ARGV and the applications defaults which are loaded from a yaml file next to the ‘$0’ application script.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/app-ctx.rb', line 22 def initialize params = {} params = { :argv => ARGV, :defaults_path => App.config_path($0) }.update(params) @argv = params[:argv] @defaults_path = params[:defaults_path] load_config_file(@defaults_path) @values, @argv = parse_command_line @argv end |
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
17 18 19 |
# File 'lib/app-ctx.rb', line 17 def argv @argv end |
#defaults_path ⇒ Object (readonly)
Returns the value of attribute defaults_path.
17 18 19 |
# File 'lib/app-ctx.rb', line 17 def defaults_path @defaults_path end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
17 18 19 |
# File 'lib/app-ctx.rb', line 17 def values @values end |
Instance Method Details
#load_config_file(cpath) ⇒ Object
overwrite current configuration with values from file
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/app-ctx.rb', line 53 def load_config_file cpath @values ||= {} if File.exists? cpath = File.(cpath) begin @values.update YAML.load_file(cpath) info "loaded(#{cpath})" debug "config values: #{@values.inspect}" rescue => e warn "failed to load: #{cpath}", e end else debug "no such config file: #{cpath}" end end |
#set_default_values(defaults) ⇒ Object
programatic way to define the default values other then from defaults file.
37 38 39 |
# File 'lib/app-ctx.rb', line 37 def set_default_values defaults @values = defaults.merge(@values) end |
#to_s ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/app-ctx.rb', line 42 def to_s <<-EOT args : #{argv.inspect} values : #{values.inspect} defaults : '#{defaults_path}' user config : '#{values[:config]}' EOT end |