Class: CreateRubyGem::Config::Store
- Inherits:
-
Object
- Object
- CreateRubyGem::Config::Store
- Defined in:
- lib/create_ruby_gem/config/store.rb
Overview
YAML persistence for presets and last-used options.
Stores configuration at ~/.config/create-ruby-gem/config.yml (or $XDG_CONFIG_HOME/create-ruby-gem/config.yml). Writes are atomic via Tempfile + rename.
Constant Summary collapse
- SCHEMA_VERSION =
Returns current config file schema version.
1
Instance Attribute Summary collapse
-
#path ⇒ String
readonly
Absolute path to the config file.
Instance Method Summary collapse
-
#delete_preset(name) ⇒ void
Deletes a named preset (no-op if it does not exist).
-
#initialize(path: nil) ⇒ Store
constructor
A new instance of Store.
-
#last_used ⇒ Hash{String => Object}
Returns the last-used option hash (empty hash if none saved).
-
#preset(name) ⇒ Hash{String => Object}?
Returns a preset by name, or
nilif it does not exist. -
#preset_names ⇒ Array<String>
Returns all preset names sorted alphabetically.
-
#save_last_used(options) ⇒ void
Persists the given options as last-used.
-
#save_preset(name, options) ⇒ void
Saves a named preset.
Constructor Details
#initialize(path: nil) ⇒ Store
Returns a new instance of Store.
19 20 21 |
# File 'lib/create_ruby_gem/config/store.rb', line 19 def initialize(path: nil) @path = path || default_path end |
Instance Attribute Details
#path ⇒ String (readonly)
Returns absolute path to the config file.
24 25 26 |
# File 'lib/create_ruby_gem/config/store.rb', line 24 def path @path end |
Instance Method Details
#delete_preset(name) ⇒ void
This method returns an undefined value.
Deletes a named preset (no-op if it does not exist).
73 74 75 76 77 |
# File 'lib/create_ruby_gem/config/store.rb', line 73 def delete_preset(name) payload = data payload.fetch('presets').delete(name.to_s) write(payload) end |
#last_used ⇒ Hash{String => Object}
Returns the last-used option hash (empty hash if none saved).
29 30 31 |
# File 'lib/create_ruby_gem/config/store.rb', line 29 def last_used data.fetch('last_used') end |
#preset(name) ⇒ Hash{String => Object}?
Returns a preset by name, or nil if it does not exist.
47 48 49 |
# File 'lib/create_ruby_gem/config/store.rb', line 47 def preset(name) data.fetch('presets').fetch(name.to_s, nil) end |
#preset_names ⇒ Array<String>
Returns all preset names sorted alphabetically.
54 55 56 |
# File 'lib/create_ruby_gem/config/store.rb', line 54 def preset_names data.fetch('presets').keys.sort end |
#save_last_used(options) ⇒ void
This method returns an undefined value.
Persists the given options as last-used.
37 38 39 40 41 |
# File 'lib/create_ruby_gem/config/store.rb', line 37 def save_last_used() payload = data payload['last_used'] = stringify_keys() write(payload) end |
#save_preset(name, options) ⇒ void
This method returns an undefined value.
Saves a named preset.
63 64 65 66 67 |
# File 'lib/create_ruby_gem/config/store.rb', line 63 def save_preset(name, ) payload = data payload.fetch('presets')[name.to_s] = stringify_keys() write(payload) end |