Class: Runby::Cli::Config
- Inherits:
-
Object
- Object
- Runby::Cli::Config
- Defined in:
- lib/runby_pace/cli/config.rb
Constant Summary collapse
- USER_CONFIG_PATH =
File.('~/.runbypace').freeze
- VALID_OPTIONS =
{ five_k_time: { validate_as: RunbyTime } }.freeze
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #known_setting?(key) ⇒ Boolean
- #load_user_settings ⇒ Object
- #option_configured?(key) ⇒ Boolean
- #pretty_print ⇒ Object
- #sanitize_value(key, value) ⇒ Object
- #store_user_settings ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
16 17 18 |
# File 'lib/runby_pace/cli/config.rb', line 16 def initialize @settings = load_user_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 store_user_settings end |
#known_setting?(key) ⇒ 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_settings ⇒ Object
20 21 22 23 24 25 26 |
# 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) ⇒ 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_print ⇒ Object
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. end value end |
#store_user_settings ⇒ Object
28 29 30 |
# 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 |