class Sia::PersistedConfig

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.

Constants

PATH

Public Class Methods

new(name=nil) click to toggle source

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

@param [#to_sym|nil] name The name of the safe to be given access.

Leave blank if Sia is to be given access.
# File lib/sia/persisted_config.rb, line 17
def initialize(name=nil)
  @access_key = name ? :"safe #{name}" : :sia
end

Public Instance Methods

delete() click to toggle source
# 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?() click to toggle source
# File lib/sia/persisted_config.rb, line 50
def exist?
  whole_hash.has_key?(@access_key)
end
options() click to toggle source

Load the persisted options from the persisted entry

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

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.

@param [Hash] opt The options to persist

# 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
refresh() click to toggle source
# File lib/sia/persisted_config.rb, line 62
def refresh
  @whole_hash = PATH.file? ? YAML.load(PATH.read) : {}
  nil
end

Private Instance Methods

whole_hash() click to toggle source

The entire persisted config hash, without access limitations

# File lib/sia/persisted_config.rb, line 71
def whole_hash
  @whole_hash || refresh || @whole_hash
end