class Harmoni::Config
Public Class Methods
Determines what type of config file should be used for a given file/path
# File lib/harmoni/config.rb, line 40 def self.detect_type(file) Config.descendants.find { |desc| desc.match?(file) }&.type || type end
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)
# File lib/harmoni/config.rb, line 48 def self.match?(file) false end
Public Instance Methods
Clear our the configuration
# File lib/harmoni/config.rb, line 98 def clear self.configuration = {} end
Delete a key or nested path from the configuration
# File lib/harmoni/config.rb, line 85 def delete(key) configuration.hpath_delete.tap do |result| save if sync_down end end
Delete the entire config file (if one exists)
# File lib/harmoni/config.rb, line 92 def delete! return true unless File.exist?(path) FileUtils.rm(path) end
Get the first matching value for the key or path
# File lib/harmoni/config.rb, line 73 def get(key) get_all(key).first end
Get all matching instancesof the key or path
# File lib/harmoni/config.rb, line 80 def get_all(key) configuration.hpath(key) end
Loads configuration from disk during synchronization
# 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 the configuration from disk and merge it in
# 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
Should persist the configuration to disk. This is adapter dependent
# 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 a single key value pair or merge in a hash. Keys can use hash path notation
# 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
Turn on sync up and down in one call for convenience
# File lib/harmoni/config.rb, line 103 def sync(toggle) self.sync_up = toggle self.sync_down = toggle end
Returns true if the sync thread is actively running and watching the file
# File lib/harmoni/config.rb, line 68 def watching? @watcher && @watcher.alive? ? true : false end
Protected Instance Methods
Applies defaults config, specified/loaded config and then overlay config
# File lib/harmoni/config.rb, line 177 def _compile(config) default_configuration.deep_merge(config).deep_merge(overlay_configuration) end
Determine what has changed after performing a reload to trigger the on_change hook.
# File lib/harmoni/config.rb, line 163 def detect_changes(hash) return unless on_change && @configuration changes = configuration.squish.to_a.diff(hash.squish.to_a).to_h.expand return if changes.empty? on_change.call(hash, changes) end
Processes configuration and converts it to a HashStruct internally
# File lib/harmoni/config.rb, line 171 def process_config(hash) return _compile(hash).to_hash_struct unless keys_to_sym? _compile(hash).keys_to_sym.to_hash_struct end
# File lib/harmoni/config.rb, line 139 def simple_postinit(*args) named = BBLib.named_args(*args) sync(true) if named[:sync] reload watch_file if sync_up? end
Spin up a thread to monitor the file for changes
# File lib/harmoni/config.rb, line 147 def watch_file if sync_up? && (@watcher.nil? || !@watcher.alive?) BBLib.logger.debug("Spinning up a configuration watcher for #{path}") @watcher = Thread.new do loop do break unless sync_up? if path && File.exist?(path) && File.mtime(path) > last_refresh reload end sleep(interval) end end end end