Class: Depot::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/depot/settings.rb

Constant Summary collapse

DEFAULTS =
{
  "warning_verbosity" => "normal",
  "theme" => "system",
  "default_install_location" => "user",
  "sandbox_preference" => "ask",
  "sandbox_profile" => "balanced",
  "sandbox_home_access" => "documents",
  "sandbox_network" => true,
  "desktop_integration" => true,
  "updates_enabled" => true
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = Paths.settings_path) ⇒ Settings

Returns a new instance of Settings.



23
24
25
# File 'lib/depot/settings.rb', line 23

def initialize(path = Paths.settings_path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



21
22
23
# File 'lib/depot/settings.rb', line 21

def path
  @path
end

Instance Method Details

#loadObject



27
28
29
30
31
32
33
34
# File 'lib/depot/settings.rb', line 27

def load
  return DEFAULTS.dup unless File.exist?(path)

  parsed = JSON.parse(File.read(path))
  DEFAULTS.merge(parsed)
rescue JSON::ParserError
  DEFAULTS.dup
end

#save(values) ⇒ Object



36
37
38
39
40
41
# File 'lib/depot/settings.rb', line 36

def save(values)
  FileUtils.mkdir_p(File.dirname(path))
  normalized = DEFAULTS.merge(values.transform_keys(&:to_s))
  File.write(path, JSON.pretty_generate(normalized) + "\n")
  normalized
end