Module: Esp::Preferences
- Defined in:
- lib/esp/preferences.rb
Overview
Per-user preferences persisted as JSON alongside the recents file at ESP_DATA_DIR/preferences.json. Today: just ‘mods_home` (the directory the Open Project picker starts in / New Project scaffolds into). More keys to come; the merge-with-defaults read keeps backward compatibility as the schema grows. Same single-writer / mutex story as Recents.
Constant Summary collapse
- DEFAULTS =
{ 'mods_home' => File.('~/Documents/ESPresso') }.freeze
Class Method Summary collapse
- .clear! ⇒ Object
- .path ⇒ Object
- .read ⇒ Object
-
.update(updates) ⇒ Object
Whitelist-merge an update.
Class Method Details
.clear! ⇒ Object
41 42 43 |
# File 'lib/esp/preferences.rb', line 41 def clear! @mutex.synchronize { write_unsafe({}) } end |
.path ⇒ Object
18 19 20 |
# File 'lib/esp/preferences.rb', line 18 def path File.join(Esp::Recents.data_dir, 'preferences.json') end |
.read ⇒ Object
22 23 24 |
# File 'lib/esp/preferences.rb', line 22 def read @mutex.synchronize { read_unsafe } end |
.update(updates) ⇒ Object
Whitelist-merge an update. Unknown keys are ignored; nil/empty values also fall through (so a UI accidentally clearing a field doesn’t blow away the default). Returns the resolved (post-merge) state.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/esp/preferences.rb', line 29 def update(updates) @mutex.synchronize do stored = read_unsafe accepted = updates.to_h .slice(*DEFAULTS.keys) .reject { |_, v| v.nil? || v.to_s.empty? } merged = stored.merge(accepted) write_unsafe(merged) merged end end |