Class: Nixenvironment::Plist

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

Constant Summary collapse

FORMAT_BINARY =
CFPropertyList228::List::FORMAT_BINARY
FORMAT_XML =
CFPropertyList228::List::FORMAT_XML
FORMAT_AUTO =

FORMAT_PLAIN = CFPropertyList228::List::FORMAT_PLAIN

CFPropertyList228::List::FORMAT_AUTO

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plist) ⇒ Plist

Returns a new instance of Plist.



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

def initialize(plist)
  @plist = plist
  @path  = @plist.filename
  @data  = CFPropertyList228.native_types(@plist.value)
end

Class Method Details

.from_file(path) ⇒ Object



11
12
13
# File 'lib/nixenvironment/plist.rb', line 11

def self.from_file(path)
  new(CFPropertyList228::List.new(:file => path))
end

.from_hash(hash) ⇒ Object



15
16
17
18
19
20
# File 'lib/nixenvironment/plist.rb', line 15

def self.from_hash(hash)
  plist        = CFPropertyList228::List.new
  plist.value  = CFPropertyList228.guess(hash)
  plist.format = FORMAT_XML
  new(plist)
end

.from_str(str) ⇒ Object



22
23
24
25
26
# File 'lib/nixenvironment/plist.rb', line 22

def self.from_str(str)
  plist = CFPropertyList228::List.new
  plist.load_str(str)
  new(plist)
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  @data[key]
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  @data[key] = value
  @plist.value = CFPropertyList228.guess(@data, :convert_unknown_to_string => true)
end

#save(path = nil, format = nil, formatted = true) ⇒ Object



43
44
45
46
47
# File 'lib/nixenvironment/plist.rb', line 43

def save(path = nil, format = nil, formatted = true)
  path ||= @path
  raise 'Path to save plist is not specified!' unless path
  @plist.save(path, format, :formatted => formatted)
end

#to_s(format = FORMAT_AUTO, formatted = true) ⇒ Object



49
50
51
# File 'lib/nixenvironment/plist.rb', line 49

def to_s(format = FORMAT_AUTO, formatted = true)
  @plist.to_str(format, :formatted => formatted)
end