class Runby::Cli::Config

Constants

USER_CONFIG_PATH
VALID_OPTIONS

Public Class Methods

new() click to toggle source
# File lib/runby_pace/cli/config.rb, line 16
def initialize
  @settings = load_user_settings
end

Public Instance Methods

[](key) click to toggle source
# File lib/runby_pace/cli/config.rb, line 32
def [](key)
  return unless known_setting?(key)
  return unless option_configured?(key)
  "#{key} => #{@settings[key]}"
end
[]=(key, value) click to toggle source
# File lib/runby_pace/cli/config.rb, line 38
def []=(key, value)
  return unless known_setting?(key)
  if value
    value = sanitize_value key, value
    return unless value
    @settings[key] = value.to_s
  else
    @settings.delete(key)
  end
  store_user_settings
end
known_setting?(key) click to toggle source
# File lib/runby_pace/cli/config.rb, line 54
def known_setting?(key)
  unless VALID_OPTIONS.key?(key.to_sym)
    puts "Unknown setting #{key}"
    return false
  end
  true
end
load_user_settings() click to toggle source
# File lib/runby_pace/cli/config.rb, line 20
def load_user_settings
  if File.exist? USER_CONFIG_PATH
    YAML.load_file USER_CONFIG_PATH
  else
    {}
  end
end
option_configured?(key) click to toggle source
# File lib/runby_pace/cli/config.rb, line 73
def option_configured?(key)
  unless @settings.key? key
    puts "#{key} not configured. Set with:\n\trunbypace --config #{key} VALUE"
    false
  end
  true
end
pretty_print() click to toggle source
# File lib/runby_pace/cli/config.rb, line 50
def pretty_print
  pp @settings
end
sanitize_value(key, value) click to toggle source
# File lib/runby_pace/cli/config.rb, line 62
def sanitize_value(key, value)
  cls = VALID_OPTIONS[key.to_sym][:validate_as]
  begin
    value = Runby.sanitize(value).as(cls)
  rescue StandardError => ex
    value = nil
    p ex.message
  end
  value
end
store_user_settings() click to toggle source
# File lib/runby_pace/cli/config.rb, line 28
def store_user_settings
  File.open(USER_CONFIG_PATH, 'w') { |file| file.write @settings.to_yaml }
end