Class: Runby::Cli::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/runby_pace/cli/config.rb

Constant Summary collapse

USER_CONFIG_PATH =
File.expand_path('~/.runbypace').freeze
VALID_OPTIONS =
{
  five_k_time: { validate_as: RunbyTime }
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



16
17
18
# File 'lib/runby_pace/cli/config.rb', line 16

def initialize
  @settings = 
end

Instance Method Details

#[](key) ⇒ Object



32
33
34
35
36
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# 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
  
end

#known_setting?(key) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# 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_settingsObject



20
21
22
23
24
25
26
# File 'lib/runby_pace/cli/config.rb', line 20

def 
  if File.exist? USER_CONFIG_PATH
    YAML.load_file USER_CONFIG_PATH
  else
    {}
  end
end

#option_configured?(key) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# 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_printObject



50
51
52
# File 'lib/runby_pace/cli/config.rb', line 50

def pretty_print
  pp @settings
end

#sanitize_value(key, value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# 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_settingsObject



28
29
30
# File 'lib/runby_pace/cli/config.rb', line 28

def 
  File.open(USER_CONFIG_PATH, 'w') { |file| file.write @settings.to_yaml }
end