Class: HammerCLI::Settings
- Inherits:
-
Object
- Object
- HammerCLI::Settings
- Defined in:
- lib/hammer_cli/settings.rb
Constant Summary collapse
- CFG_PATH =
['~/.hammer/', '/etc/hammer/', "#{::RbConfig::CONFIG['sysconfdir']}/hammer/"].uniq
- CFG_PATH_LOCAL =
['./config/']
Class Method Summary collapse
- .clear ⇒ Object
- .deep_merge!(h, other_h) ⇒ Object
- .default_settings ⇒ Object
- .dump ⇒ Object
- .empty ⇒ Object
- .get(*keys) ⇒ Object
- .load(settings_hash) ⇒ Object
- .load_from_defaults ⇒ Object
- .load_from_file(file_path) ⇒ Object
- .load_from_paths(files) ⇒ Object
- .path_history ⇒ Object
- .settings ⇒ Object
- .symbolize_hash(h) ⇒ Object
Class Method Details
.clear ⇒ Object
55 56 57 58 |
# File 'lib/hammer_cli/settings.rb', line 55 def self.clear empty load(default_settings) end |
.deep_merge!(h, other_h) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/hammer_cli/settings.rb', line 88 def self.deep_merge!(h, other_h) other_h = symbolize_hash(other_h) h.merge!(other_h) do |key, old_val, new_val| if old_val.is_a? Hash and new_val.is_a? Hash deep_merge!(old_val, new_val) elsif old_val.is_a? Array and new_val.is_a? Array old_val += new_val else new_val end end end |
.default_settings ⇒ Object
74 75 76 77 78 79 |
# File 'lib/hammer_cli/settings.rb', line 74 def self.default_settings { :use_defaults => true, :completion_cache_file => '~/.cache/hammer_completion.json' } end |
.dump ⇒ Object
65 66 67 |
# File 'lib/hammer_cli/settings.rb', line 65 def self.dump settings end |
.empty ⇒ Object
60 61 62 63 |
# File 'lib/hammer_cli/settings.rb', line 60 def self.empty settings.clear path_history.clear end |
.get(*keys) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/hammer_cli/settings.rb', line 10 def self.get(*keys) keys.inject(settings) do |value, key| return nil unless value value[key.to_sym] end end |
.load(settings_hash) ⇒ Object
51 52 53 |
# File 'lib/hammer_cli/settings.rb', line 51 def self.load(settings_hash) deep_merge!(settings, settings_hash) end |
.load_from_defaults ⇒ Object
46 47 48 49 |
# File 'lib/hammer_cli/settings.rb', line 46 def self.load_from_defaults load_from_paths CFG_PATH load_from_paths CFG_PATH_LOCAL end |
.load_from_file(file_path) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/hammer_cli/settings.rb', line 32 def self.load_from_file(file_path) if File.file? file_path begin config = YAML::load(File.open(file_path)) if config load(config) path_history << file_path end rescue Exception => e warn _("Warning: Couldn't load configuration file %{path}: %{message}.") % { path: file_path, message: e. } end end end |
.load_from_paths(files) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/hammer_cli/settings.rb', line 17 def self.load_from_paths(files) files.reverse.each do |path| full_path = File. path if File.directory? full_path # check for cli_config.yml load_from_file(File.join(full_path, 'cli_config.yml')) load_from_file(File.join(full_path, 'defaults.yml')) # load config for modules Dir.glob(File.join(full_path, 'cli.modules.d/*.yml')).sort.each do |f| load_from_file(f) end end end end |
.path_history ⇒ Object
69 70 71 72 |
# File 'lib/hammer_cli/settings.rb', line 69 def self.path_history @path_history ||= [] @path_history end |
.settings ⇒ Object
83 84 85 86 |
# File 'lib/hammer_cli/settings.rb', line 83 def self.settings @settings_hash ||= default_settings @settings_hash end |
.symbolize_hash(h) ⇒ Object
102 103 104 |
# File 'lib/hammer_cli/settings.rb', line 102 def self.symbolize_hash(h) h = h.inject({}) { |sym_hash,(k,v)| sym_hash.update(k.to_sym => v) } end |