Module: Bcpm::Config
- Defined in:
- lib/bcpm/config.rb
Overview
Persistent, per-user bcpm configuration information.
Class Method Summary collapse
-
.[](key) ⇒ Object
Hash-style access to the configuration dictionary.
-
.[]=(key, new_value) ⇒ Object
Hash-style access to configuration dictionary.
-
.config ⇒ Object
The configuration dictionary.
-
.config_file ⇒ Object
Path to the configuration YAML file.
-
.print_config ⇒ Object
Outputs the configuration.
-
.read_config ⇒ Object
Reads the YAML configuration file.
-
.reset ⇒ Object
Removes the configuration YAML file and resets the configuration hash.
-
.ui_set(key, value) ⇒ Object
Executes a config command from the UI.
-
.write_config ⇒ Object
Writes the configuration to the YAML file.
Class Method Details
.[](key) ⇒ Object
Hash-style access to the configuration dictionary.
9 10 11 |
# File 'lib/bcpm/config.rb', line 9 def self.[](key) config[key.to_sym] end |
.[]=(key, new_value) ⇒ Object
Hash-style access to configuration dictionary.
14 15 16 17 18 19 20 21 |
# File 'lib/bcpm/config.rb', line 14 def self.[]=(key, new_value) if new_value.nil? config.delete key.to_sym else config[key.to_sym] = new_value end write_config end |
.config ⇒ Object
The configuration dictionary.
24 25 26 |
# File 'lib/bcpm/config.rb', line 24 def self.config @config ||= read_config end |
.config_file ⇒ Object
Path to the configuration YAML file.
43 44 45 |
# File 'lib/bcpm/config.rb', line 43 def self.config_file File. '~/.bcpm_config' end |
.print_config ⇒ Object
Outputs the configuration.
54 55 56 57 58 |
# File 'lib/bcpm/config.rb', line 54 def self.print_config config.keys.sort_by(&:to_s).each do |key| print "#{key}: #{config[key]}\n" end end |
.read_config ⇒ Object
Reads the YAML configuration file.
29 30 31 32 33 34 35 |
# File 'lib/bcpm/config.rb', line 29 def self.read_config if File.exists? config_file @config = File.open(config_file) { |f| YAML.load f } else @config = {} end end |
.reset ⇒ Object
Removes the configuration YAML file and resets the configuration hash.
48 49 50 51 |
# File 'lib/bcpm/config.rb', line 48 def self.reset File.unlink config_file if File.exist?(config_file) @config = nil end |
.ui_set(key, value) ⇒ Object
Executes a config command from the UI.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/bcpm/config.rb', line 61 def self.ui_set(key, value) # Normalize values. case value && value.downcase when 'on', 'true' value = true when 'off', 'false' value = false end # Process keys that include values. prefix = key[0] toggle = false if [?+, ?-, ?^].include? prefix key = key[1..-1] case prefix when ?+ value = true when ?- value = false when ?^ toggle = true end self[key] end # Normalize keys. key = key.downcase # Execute the configuration change. if toggle self[key] = !self[key] else self[key] = value end print "#{key} set to #{value}\n" end |
.write_config ⇒ Object
Writes the configuration to the YAML file.
38 39 40 |
# File 'lib/bcpm/config.rb', line 38 def self.write_config File.open(config_file, 'wb') { |f| YAML.dump config, f } end |