Module: GDK::ConfigSettings::Persisted

Defined in:
lib/gdk/config_settings.rb

Overview

This module contains methods to read and write a config file.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(klass) ⇒ Object



257
258
259
260
261
262
# File 'lib/gdk/config_settings.rb', line 257

def self.new(klass)
  config = klass.new
  config.extend Persisted
  config.load_yaml!
  config
end

Instance Method Details

#load_yaml!Object



264
265
266
267
268
269
270
271
# File 'lib/gdk/config_settings.rb', line 264

def load_yaml!
  return unless file_exist?

  assign_mtime!

  raw_yaml = File.read(self.class::FILE)
  @yaml = YAML.safe_load(raw_yaml) || {}
end

#reloadObject



273
274
275
# File 'lib/gdk/config_settings.rb', line 273

def reload
  load_yaml!
end

#save_yaml!Object



277
278
279
280
281
282
283
284
285
286
287
# File 'lib/gdk/config_settings.rb', line 277

def save_yaml!
  if file_exist? && File.mtime(self.class::FILE) != @__yaml_mtime # rubocop:disable Style/IfUnlessModifier
    raise YamlModified, "Config YAML has been modified since it was loaded. Reload it before saving"
  end

  Backup.new(self.class::FILE).backup! if file_exist?
  File.write(self.class::FILE, dump!(user_only: true).to_yaml)
  assign_mtime! if file_exist?

  nil
end