Module: Echoes::Preferences

Defined in:
lib/echoes/preferences.rb

Overview

Thin wrapper around ‘[NSUserDefaults initWithSuiteName:]` for cross-launch persistence of small user-tweaked values (font size, …). The on-disk plist lives at `~/Library/Preferences/<SUITE>.plist` and `defaults read|write|delete <SUITE>` from the shell works as expected.

We use an explicit suite name rather than ‘standardUserDefaults` because echoes runs under `bundle exec exe/echoes` (no Info.plist bundle ID) and as a future `.app`. A fixed suite gives stable storage across both.

Constant Summary collapse

SUITE =
'jp.dio.echoes'

Class Method Summary collapse

Class Method Details

.defaultsObject



19
20
21
22
23
24
# File 'lib/echoes/preferences.rb', line 19

def self.defaults
  @defaults ||= begin
    obj = ObjC::MSG_PTR.call(ObjC.cls('NSUserDefaults'), ObjC.sel('alloc'))
    ObjC::MSG_PTR_1.call(obj, ObjC.sel('initWithSuiteName:'), ObjC.nsstring(SUITE))
  end
end

.delete(key) ⇒ Object



40
41
42
43
# File 'lib/echoes/preferences.rb', line 40

def self.delete(key)
  ObjC::MSG_VOID_1.call(defaults, ObjC.sel('removeObjectForKey:'),
                        ObjC.nsstring(key.to_s))
end

.fetch_double(key, default:) ⇒ Object

Returns the stored Float for ‘key`, or `default` if the key isn’t set. We round-trip through ‘objectForKey:` so we can distinguish “missing” from “set to 0.0” — `doubleForKey:` collapses both.



29
30
31
32
33
# File 'lib/echoes/preferences.rb', line 29

def self.fetch_double(key, default:)
  obj = ObjC::MSG_PTR_1.call(defaults, ObjC.sel('objectForKey:'), ObjC.nsstring(key.to_s))
  return default if obj.null?
  ObjC::MSG_RET_D.call(obj, ObjC.sel('doubleValue'))
end

.set_double(key, value) ⇒ Object



35
36
37
38
# File 'lib/echoes/preferences.rb', line 35

def self.set_double(key, value)
  ObjC::MSG_VOID_D_1.call(defaults, ObjC.sel('setDouble:forKey:'),
                          value.to_f, ObjC.nsstring(key.to_s))
end