Class: CLIUtils::Configurator
- Inherits:
-
Object
- Object
- CLIUtils::Configurator
- Defined in:
- lib/cliutils/configurator.rb
Overview
Configuration Class
Manages any configuration values and the flat YAML file
into which they get stored.
Instance Attribute Summary collapse
-
#config_path ⇒ String
readonly
Stores the path to the configuration file.
-
#current_version ⇒ Symbol
Stores the Configurator key that refers to the current configuration version.
-
#data ⇒ Hash
readonly
Stores the configuration data itself.
-
#last_version ⇒ Symbol
Stores the Configurator key that refers to the value at which the app last changed config versions.
-
#version_section ⇒ Symbol
Stores the section that contains the version keys.
Instance Method Summary collapse
-
#add_section(section_name) ⇒ void
Adds a new section to the config file (if it doesn’t already exist).
-
#backup ⇒ String
Convenience method to backup the configuration file in the same directory that the original inhabits.
-
#compare_version ⇒ void
Compares the current version (if it exists) to the last version that needed a configuration change (if it exists).
-
#delete_section(section_name) ⇒ void
Removes a section to the config file (if it exists).
-
#ingest_prefs(prefs) ⇒ void
Ingests a Prefs class and adds its answers to the configuration data.
-
#initialize(path) ⇒ void
constructor
Initializes configuration from a flat file.
-
#method_missing(name, *args) { ... } ⇒ Hash
Hook that fires when a non-existent method is called.
-
#reset ⇒ void
Clears the configuration data.
-
#save ⇒ void
Saves the configuration data to the previously stored flat file.
Constructor Details
#initialize(path) ⇒ void
Initializes configuration from a flat file.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/cliutils/configurator.rb', line 36 def initialize(path) _path = File.(path) @config_path = _path @data = {} if File.file?(_path) data = YAML.load_file(_path) @data.deep_merge!(data).deep_symbolize_keys! end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) { ... } ⇒ Hash
Hook that fires when a non-existent method is called. Allows this module to return data from the config Hash when given a method name that matches a key.
117 118 119 120 121 122 123 |
# File 'lib/cliutils/configurator.rb', line 117 def method_missing(name, *args, &block) if name[-1,1] == '=' @data[name[0..-2].to_sym] = args[0] else @data[name.to_sym] ||= {} end end |
Instance Attribute Details
#config_path ⇒ String (readonly)
Stores the path to the configuration file.
22 23 24 |
# File 'lib/cliutils/configurator.rb', line 22 def config_path @config_path end |
#current_version ⇒ Symbol
Stores the Configurator key that refers to the current configuration version.
12 13 14 |
# File 'lib/cliutils/configurator.rb', line 12 def current_version @current_version end |
#data ⇒ Hash (readonly)
Stores the configuration data itself.
26 27 28 |
# File 'lib/cliutils/configurator.rb', line 26 def data @data end |
#last_version ⇒ Symbol
Stores the Configurator key that refers to the value at which the app last changed config versions.
18 19 20 |
# File 'lib/cliutils/configurator.rb', line 18 def last_version @last_version end |
#version_section ⇒ Symbol
Stores the section that contains the version keys.
31 32 33 |
# File 'lib/cliutils/configurator.rb', line 31 def version_section @version_section end |
Instance Method Details
#add_section(section_name) ⇒ void
This method returns an undefined value.
Adds a new section to the config file (if it doesn’t already exist).
51 52 53 54 55 56 57 |
# File 'lib/cliutils/configurator.rb', line 51 def add_section(section_name) if !@data.key?(section_name) @data[section_name] = {} else fail "Section already exists: #{ section_name }" end end |
#backup ⇒ String
Convenience method to backup the configuration file in the same directory that the original inhabits.
62 63 64 65 66 67 68 69 |
# File 'lib/cliutils/configurator.rb', line 62 def backup backup_path = '' unless @config_path.nil? || @config_path.empty? backup_path = "#{ @config_path }-#{ Time.now.to_i }" FileUtils.cp(@config_path, backup_path) end backup_path end |
#compare_version ⇒ void
This method returns an undefined value.
Compares the current version (if it exists) to the last version that needed a configuration change (if it exists). Assuming they exist and that the current version is behind the “last-config” version, execute a passed block.
77 78 79 80 81 82 83 84 |
# File 'lib/cliutils/configurator.rb', line 77 def compare_version c_version = Gem::Version.new(@current_version) l_version = Gem::Version.new(@last_version) if @current_version.nil? || c_version < l_version yield @current_version, @last_version end end |
#delete_section(section_name) ⇒ void
This method returns an undefined value.
Removes a section to the config file (if it exists).
89 90 91 92 93 94 95 |
# File 'lib/cliutils/configurator.rb', line 89 def delete_section(section_name) if @data.key?(section_name) @data.delete(section_name) else fail "Cannot delete nonexistent section: #{ section_name }" end end |
#ingest_prefs(prefs) ⇒ void
This method returns an undefined value.
Ingests a Prefs class and adds its answers to the configuration data.
101 102 103 104 105 106 107 108 |
# File 'lib/cliutils/configurator.rb', line 101 def ingest_prefs(prefs) fail 'Invaid Prefs class' unless prefs.kind_of?(Prefs) prefs.prompts.each do |p| section_sym = p.config_section.to_sym add_section(section_sym) unless @data.key?(section_sym) @data[section_sym].merge!(p.config_key.to_sym => p.answer) end end |
#reset ⇒ void
This method returns an undefined value.
Clears the configuration data.
127 128 129 |
# File 'lib/cliutils/configurator.rb', line 127 def reset @data = {} end |
#save ⇒ void
This method returns an undefined value.
Saves the configuration data to the previously stored flat file.
134 135 136 137 138 |
# File 'lib/cliutils/configurator.rb', line 134 def save File.open(@config_path, 'w') do |f| f.write(@data.deep_stringify_keys.to_yaml) end end |