Class: Sia::PersistedConfig
- Inherits:
-
Object
- Object
- Sia::PersistedConfig
- 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
- #delete ⇒ Object
- #exist? ⇒ Boolean
-
#initialize(name = nil) ⇒ PersistedConfig
constructor
Provide a name for safe-specific access.
-
#options ⇒ Object
Load the persisted options from the persisted entry.
-
#persist(opt) ⇒ Object
Persist some options into the persisted entry.
- #refresh ⇒ Object
Constructor Details
#initialize(name = nil) ⇒ PersistedConfig
Provide a name for safe-specific access. Call w/o args for Sia-wide 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
#delete ⇒ Object
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
50 51 52 |
# File 'lib/sia/persisted_config.rb', line 50 def exist? whole_hash.has_key?(@access_key) end |
#options ⇒ Object
Load the persisted options from the persisted entry
42 43 44 45 46 47 48 |
# File 'lib/sia/persisted_config.rb', line 42 def entry = whole_hash.fetch(@access_key) do @access_key == :sia ? {} : Sia. 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.
32 33 34 35 36 37 38 |
# File 'lib/sia/persisted_config.rb', line 32 def persist(opt) opt = Configurable::DEFAULTS.merge().merge(opt) whole_hash.merge!(@access_key => opt) PATH.write(YAML.dump(whole_hash)) end |