class Plister::Plist

Constants

TYPES

Attributes

domain[RW]
type[RW]

Public Class Methods

new(domain, type: 'user') click to toggle source
# 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

Public Instance Methods

exists?() click to toggle source
# File lib/plister/plist.rb, line 34
def exists?
  File.exist?(path)
end
merge(prefs) click to toggle source
# File lib/plister/plist.rb, line 25
def merge(prefs)
  self.preferences = preferences.deep_merge(prefs)
end
preferences() click to toggle source
# File lib/plister/plist.rb, line 12
def preferences
  @preferences ||= CFPropertyList.native_types(list.value)
rescue CFFormatError
  {}
end
Also aliased as: to_h
preferences=(prefs) click to toggle source
# 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?() click to toggle source
# File lib/plister/plist.rb, line 42
def readable?
  File.readable?(path)
end
to_h()
Alias for: preferences
writable?() click to toggle source
# File lib/plister/plist.rb, line 38
def writable?
  File.writable?(path)
end
write() click to toggle source
# File lib/plister/plist.rb, line 29
def write
  raise IOError, "#{path} is not writable by #{Plister.user}" unless writable?
  list.save
end

Private Instance Methods

list() click to toggle source
# File lib/plister/plist.rb, line 61
def list
  raise IOError, "#{path} does not exist" unless exists?
  raise IOError, "#{path} is not readable by #{Plister.user}" unless readable?
  @list ||= CFPropertyList::List.new file: path
end
normalize_domain(domain) click to toggle source
# File lib/plister/plist.rb, line 71
def normalize_domain(domain)
  domain = File.basename(domain)
  domain = domain.sub(/\.plist\z/, '')
  domain = domain.sub(/\.#{Plister.uuid}\z/, '')
  domain
end
path() click to toggle source
# File lib/plister/plist.rb, line 48
def path
  @path ||= begin
    case type
    when 'system'
      "/Library/Preferences/#{domain}.plist"
    when 'user'
      "/Users/#{Plister.user}/Library/Preferences/#{domain}.plist"
    when 'host'
      "/Users/#{Plister.user}/Library/preferences/ByHost/#{domain}.#{Plister.uuid}.plist"
    end
  end
end
valid_type?() click to toggle source
# File lib/plister/plist.rb, line 67
def valid_type?
  TYPES.include?(type)
end