class Harmoni::Config

Public Class Methods

detect_type(file) click to toggle source

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
match?(file) click to toggle source

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

[](key)
Alias for: get
[]=(key = nil, value = nil, **opts)
Alias for: set
clear() click to toggle source

Clear our the configuration

# File lib/harmoni/config.rb, line 98
def clear
  self.configuration = {}
end
delete(key) click to toggle source

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!() click to toggle source

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(key) click to toggle source

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
Also aliased as: []
get_all(key) click to toggle source

Get all matching instancesof the key or path

# File lib/harmoni/config.rb, line 80
def get_all(key)
  configuration.hpath(key)
end
load_config() click to toggle source

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() click to toggle source

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
save() click to toggle source

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(key = nil, value = nil, **opts) click to toggle source

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
Also aliased as: []=
sync(toggle) click to toggle source

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
Also aliased as: sync=
sync=(toggle)
Alias for: sync
watching?() click to toggle source

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

_compile(config) click to toggle source

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
detect_changes(hash) click to toggle source

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
process_config(hash) click to toggle source

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
simple_postinit(*args) click to toggle source
# 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
watch_file() click to toggle source

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