Class: Plister::Plist

Inherits:
Object
  • Object
show all
Defined in:
lib/plister/plist.rb

Constant Summary collapse

TYPES =
%w(user system host).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain, type: 'user') ⇒ Plist

Returns a new instance of Plist.

Raises:

  • (ArgumentError)


6
7
8
9
10
# File 'lib/plister/plist.rb', line 6

def initialize(domain, type: 'user')
  @domain = normalize_domain(domain)
  @type   = type.to_s
  raise ArgumentError, 'Invalid type' unless valid_type?
end

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



3
4
5
# File 'lib/plister/plist.rb', line 3

def domain
  @domain
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/plister/plist.rb', line 3

def type
  @type
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/plister/plist.rb', line 34

def exists?
  File.exist?(path)
end

#merge(prefs) ⇒ Object



25
26
27
# File 'lib/plister/plist.rb', line 25

def merge(prefs)
  self.preferences = preferences.deep_merge(prefs)
end

#preferencesObject Also known as: to_h



12
13
14
15
16
# File 'lib/plister/plist.rb', line 12

def preferences
  @preferences ||= CFPropertyList.native_types(list.value)
rescue CFFormatError
  {}
end

#preferences=(prefs) ⇒ Object



19
20
21
22
23
# File 'lib/plister/plist.rb', line 19

def preferences=(prefs)
  list.value = CFPropertyList.guess(prefs, convert_unknown_to_string: true)
  @preferenes = nil
  preferences
end

#readable?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/plister/plist.rb', line 42

def readable?
  File.readable?(path)
end

#writable?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/plister/plist.rb', line 38

def writable?
  File.writable?(path)
end

#writeObject

Raises:

  • (IOError)


29
30
31
32
# File 'lib/plister/plist.rb', line 29

def write
  raise IOError, "#{path} is not writable by #{Plister.user}" unless writable?
  list.save
end