Class: Sia::PersistedConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/sia/persisted_config.rb

Overview

Sia-wide and safe-specific persisted config

Sia can read and write Sia-wide persisted config, but can't read or write any safe-specific config. Safes can read Sia-wide config, and can read and write their own config, but can't access other safes' configs.

Constant Summary collapse

PATH =
Pathname(Dir.home) / '.sia_config'

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ PersistedConfig

Provide a name for safe-specific access. Call w/o args for Sia-wide access

Parameters:

  • name (#to_sym|nil) (defaults to: nil)

    The name of the safe to be given access. Leave blank if Sia is to be given access.



17
18
19
# File 'lib/sia/persisted_config.rb', line 17

def initialize(name=nil)
  @access_key = name ? :"safe #{name}" : :sia
end

Instance Method Details

#deleteObject



54
55
56
57
58
59
60
# File 'lib/sia/persisted_config.rb', line 54

def delete
  return unless exist?

  whole_hash.delete(@access_key)

  PATH.write(YAML.dump(whole_hash))
end

#exist?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/sia/persisted_config.rb', line 50

def exist?
  whole_hash.has_key?(@access_key)
end

#optionsObject

Load the persisted options from the persisted entry



42
43
44
45
46
47
48
# File 'lib/sia/persisted_config.rb', line 42

def options
  entry = whole_hash.fetch(@access_key) do
    @access_key == :sia ? {} : Sia.options
  end

  Configurable::DEFAULTS.merge(entry)
end

#persist(opt) ⇒ Object

Persist some options into the persisted entry

An attempt is made to not over-write things on accident by merging the previously persisted options with Sia's defaults, and merging the provided options into the result. This way, providing partial updates to the options won't over-write all the other options, and if new options are added to the gem later they will be effortlessly merged into the persisted config without having to pass them explicitly.

Parameters:

  • opt (Hash)

    The options to persist



32
33
34
35
36
37
38
# File 'lib/sia/persisted_config.rb', line 32

def persist(opt)
  opt = Configurable::DEFAULTS.merge(options).merge(opt)

  whole_hash.merge!(@access_key => opt)

  PATH.write(YAML.dump(whole_hash))
end

#refreshObject



62
63
64
65
# File 'lib/sia/persisted_config.rb', line 62

def refresh
  @whole_hash = PATH.file? ? YAML.load(PATH.read) : {}
  nil
end