Class: Harmoni::Config
- Inherits:
-
Object
- Object
- Harmoni::Config
- Includes:
- BBLib::Delegator, BBLib::Effortless, BBLib::TypeInit
- Defined in:
- lib/harmoni/config.rb
Class Method Summary collapse
-
.detect_type(file) ⇒ Object
Determines what type of config file should be used for a given file/path.
-
.match?(file) ⇒ Boolean
Should be overriden in subclasses.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear our the configuration.
-
#delete(key) ⇒ Object
Delete a key or nested path from the configuration.
-
#delete! ⇒ Object
Delete the entire config file (if one exists).
-
#get(key) ⇒ Object
(also: #[])
Get the first matching value for the key or path.
-
#get_all(key) ⇒ Object
Get all matching instancesof the key or path.
-
#load_config ⇒ Object
Loads configuration from disk during synchronization.
-
#reload ⇒ Object
Reload the configuration from disk and merge it in.
-
#save ⇒ Object
Should persist the configuration to disk.
-
#set(key = nil, value = nil, **opts) ⇒ Object
(also: #[]=)
Set a single key value pair or merge in a hash.
-
#sync(toggle) ⇒ Object
(also: #sync=)
Turn on sync up and down in one call for convenience.
-
#watching? ⇒ Boolean
Returns true if the sync thread is actively running and watching the file.
Class Method Details
.detect_type(file) ⇒ Object
Determines what type of config file should be used for a given file/path
40 41 42 |
# File 'lib/harmoni/config.rb', line 40 def self.detect_type(file) Config.descendants.find { |desc| desc.match?(file) }&.type || type end |
.match?(file) ⇒ Boolean
Should be overriden in subclasses. This is used to determine whether the file is the correct format for the class. For example, the yaml class should use this to determine if the config file is yaml (if it exists) or should be yaml (based on extension if it does not exist)
48 49 50 |
# File 'lib/harmoni/config.rb', line 48 def self.match?(file) false end |
Instance Method Details
#clear ⇒ Object
Clear our the configuration
98 99 100 |
# File 'lib/harmoni/config.rb', line 98 def clear self.configuration = {} end |
#delete(key) ⇒ Object
Delete a key or nested path from the configuration
85 86 87 88 89 |
# File 'lib/harmoni/config.rb', line 85 def delete(key) configuration.hpath_delete.tap do |result| save if sync_down end end |
#delete! ⇒ Object
Delete the entire config file (if one exists)
92 93 94 95 |
# File 'lib/harmoni/config.rb', line 92 def delete! return true unless File.exist?(path) FileUtils.rm(path) end |
#get(key) ⇒ Object Also known as: []
Get the first matching value for the key or path
73 74 75 |
# File 'lib/harmoni/config.rb', line 73 def get(key) get_all(key).first end |
#get_all(key) ⇒ Object
Get all matching instancesof the key or path
80 81 82 |
# File 'lib/harmoni/config.rb', line 80 def get_all(key) configuration.hpath(key) end |
#load_config ⇒ Object
Loads configuration from disk during synchronization
117 118 119 120 121 |
# File 'lib/harmoni/config.rb', line 117 def load_config # Nothing in base class. This should be used to load the configuration from # disk if saved to a file. {} end |
#reload ⇒ Object
Reload the configuration from disk and merge it in
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/harmoni/config.rb', line 124 def reload if !persist_memory? self.configuration = load_config elsif prefer_memory self.configuration = load_config.deep_merge(configuration) else self.configuration = configuration.deep_merge(load_config) end self.last_refresh = Time.now on_reload.call(configuration) if on_reload true end |
#save ⇒ Object
Should persist the configuration to disk. This is adapter dependent
111 112 113 114 |
# File 'lib/harmoni/config.rb', line 111 def save # Nothing in base class. This should be used to persist settings in # subclasses that use files. end |
#set(key = nil, value = nil, **opts) ⇒ Object Also known as: []=
Set a single key value pair or merge in a hash. Keys can use hash path notation
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/harmoni/config.rb', line 53 def set(key = nil, value = nil, **opts) if key configuration.hpath_set(key => value) else opts.each do |k, v| configuration.hpath_set(k => v) end end save if sync_down true end |
#sync(toggle) ⇒ Object Also known as: sync=
Turn on sync up and down in one call for convenience
103 104 105 106 |
# File 'lib/harmoni/config.rb', line 103 def sync(toggle) self.sync_up = toggle self.sync_down = toggle end |
#watching? ⇒ Boolean
Returns true if the sync thread is actively running and watching the file
68 69 70 |
# File 'lib/harmoni/config.rb', line 68 def watching? @watcher && @watcher.alive? ? true : false end |