Module: Kerbi::ConfigFile
- Defined in:
- lib/config/config_file.rb
Constant Summary collapse
- DIR_NAME =
".kerbi"
- FILE_NAME =
"config.yaml"
Class Method Summary collapse
- .create_file_if_missing ⇒ Object
- .dir_path ⇒ Object
- .file_path ⇒ Object
- .legal_keys ⇒ Object
- .patch(config) ⇒ Object
- .read ⇒ Hash{Symbol, String}
- .reset ⇒ Object
- .write(config, skip_check: false) ⇒ Object
Class Method Details
.create_file_if_missing ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/config/config_file.rb', line 16 def self.create_file_if_missing unless File.exists?(file_path) unless Dir.exists?(dir_path) Dir.mkdir(dir_path) end write({}, skip_check: true) end end |
.dir_path ⇒ Object
12 13 14 |
# File 'lib/config/config_file.rb', line 12 def self.dir_path "#{Dir.home}/#{DIR_NAME}" end |
.file_path ⇒ Object
8 9 10 |
# File 'lib/config/config_file.rb', line 8 def self.file_path "#{Dir.home}/#{DIR_NAME}/#{FILE_NAME}" end |
.legal_keys ⇒ Object
56 57 58 |
# File 'lib/config/config_file.rb', line 56 def self.legal_keys Kerbi::Consts::OptionKeys::LEGAL_CONFIG_FILE_KEYS end |
.patch(config) ⇒ Object
45 46 47 48 49 |
# File 'lib/config/config_file.rb', line 45 def self.patch(config) existing_config = read new_config = existing_config.merge(config) write(new_config) end |
.read ⇒ Hash{Symbol, String}
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/config/config_file.rb', line 26 def self.read begin create_file_if_missing contents = YAML.load_file(file_path) contents.slice(*legal_keys) rescue StandardError => e puts "[WARN] failed to read config #{file_path} (#{e})" {} end end |
.reset ⇒ Object
51 52 53 54 |
# File 'lib/config/config_file.rb', line 51 def self.reset create_file_if_missing write({}) end |
.write(config, skip_check: false) ⇒ Object
38 39 40 41 42 |
# File 'lib/config/config_file.rb', line 38 def self.write(config, skip_check: false) create_file_if_missing unless skip_check config = config.deep_dup.stringify_keys.slice(*legal_keys) File.write(file_path, YAML.dump(config)) end |