Class: Harmoni::Config

Inherits:
Object
  • Object
show all
Includes:
BBLib::Delegator, BBLib::Effortless, BBLib::TypeInit
Defined in:
lib/harmoni/config.rb

Direct Known Subclasses

JSON, YAML

Class Method Summary collapse

Instance Method Summary collapse

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)

Returns:

  • (Boolean)


48
49
50
# File 'lib/harmoni/config.rb', line 48

def self.match?(file)
  false
end

Instance Method Details

#clearObject

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_configObject

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

#reloadObject

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

#saveObject

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

Returns:

  • (Boolean)


68
69
70
# File 'lib/harmoni/config.rb', line 68

def watching?
  @watcher && @watcher.alive? ? true : false
end