Module: SublimeDSL::TextMate::PList

Defined in:
lib/sublime_dsl/textmate/plist.rb

Overview

Tools to read and write hashes in PList format.

Class Method Summary collapse

Class Method Details

.dump(hash) ⇒ Object

Returns a String containing the PList XML for hash.



43
44
45
46
47
48
49
# File 'lib/sublime_dsl/textmate/plist.rb', line 43

def dump(hash)
  "    <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n    <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n    <plist version=\"1.0\">\n  XML\nend\n".dedent << dump_hash(hash, '') << '</plist>'

.export(hash, file) ⇒ Object

Writes the Hash hash to file in PList format.



19
20
21
# File 'lib/sublime_dsl/textmate/plist.rb', line 19

def export(hash, file)
  File.open(file, 'wb:utf-8') { |f| f.write dump(hash) }
end

.import(file) ⇒ Object

Returns a Hash read from file. See ::load.



14
15
16
# File 'lib/sublime_dsl/textmate/plist.rb', line 14

def import(file)
  File.open(file, 'r:utf-8') { |f| load(f) }
end

.load(string_or_io) ⇒ Object

Returns a Hash corresponding to the root dict of the PList.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sublime_dsl/textmate/plist.rb', line 24

def load(string_or_io)
  doc = Tools::XML.load(string_or_io)
  # Document name="document" children = [
  #   DTD
  #   Element name="plist" children = [
  #     Element name="dict" children = [
  #       Element name="key" children = [ Text "author" ]
  #       Element name="string" children = [ Text "Z comme Zorglub" ]
  #       Element name = "key"  children = [ Text "name" ]
  #       Element name = "string" children = [ Text "Zorgléoptère" ]
  #       ...
  #     ]
  #   ]
  # ]
  root_node = doc.children.last.children.first
  load_node(root_node)
end