module Boxlet::Config

Constants

ARGS

Public Instance Methods

populate_params!(argv, path_to_config) click to toggle source
# File lib/boxlet/config.rb, line 67
def populate_params!(argv, path_to_config)
  @raw_config = load_config_file(path_to_config)
  @raw_params = parse_arguments(argv)

  @config = @raw_params
  @config[:debug] = @raw_config[:debug] || @raw_params[:debug]
end
symbolize_keys(hash) click to toggle source
# File lib/boxlet/config.rb, line 75
def symbolize_keys(hash)
  hash.inject({}){|result, (key, value)|
    new_key = key.instance_of?(String) ? key.to_sym : key
    new_value = value.instance_of?(Hash) ? symbolize_keys(value) : value

    result[new_key] = new_value
    result
  }
end

Private Instance Methods

load_config_file(path_to_config) click to toggle source
# File lib/boxlet/config.rb, line 111
def load_config_file(path_to_config)
  begin
    loaded_config = YAML.load_file(path_to_config)
    symbolize_keys(loaded_config)
  rescue
    Boxlet.log(:warn, "Error loading config file!  Using defaults...")
    {}
  end
end
parse_arguments(argv) click to toggle source
# File lib/boxlet/config.rb, line 87
def parse_arguments(argv)
  params = @raw_config

  ARGS.each_pair do |param_name, param_attrs|
    param_short_name = param_attrs[:short]
    config_value = @raw_config[param_name.to_sym]
    default = param_attrs[:default]
    sanitizer = param_attrs[:sanitizer]

    param_value = argv["--#{param_name}"] ||
                  (param_short_name.nil? ? nil : argv["-#{param_short_name}"]) ||
                  (config_value.nil? ? nil : config_value) ||
                  (default.is_a?(Proc) ? default.call : default)

    param_value = sanitizer.call(param_value) if sanitizer.is_a?(Proc)

    if !param_value.nil?
      params[param_name] = param_value
    end
  end

  params
end