Namespace

Plist::Emit

Create a plist

You can dump an object to a plist in one of two ways:

The following Ruby classes are converted into native plist types:

  Array, Bignum, Date, DateTime, Fixnum, Float, Hash, Integer, String, Symbol, Time, true, false

For detailed usage instructions, refer to USAGE and the methods documented below.

Public Class Methods

dump(obj, envelope = true) click to toggle source

The following Ruby classes are converted into native plist types:

  Array, Bignum, Date, DateTime, Fixnum, Float, Hash, Integer, String, Symbol, Time

Write us (via RubyForge) if you think another class can be coerced safely into one of the expected plist classes.

IO and StringIO objects are encoded and placed in elements; other objects are Marshal.dump‘ed unless they implement to_plist_node.

The envelope parameters dictates whether or not the resultant plist fragment is wrapped in the normal XML/plist header and footer. Set it to false if you only want the fragment.

    # File lib/plist/generator.rb, line 44
44:   def self.dump(obj, envelope = true)
45:     output = plist_node(obj)
46: 
47:     output = wrap(output) if envelope
48: 
49:     return output
50:   end
save_plist(obj, filename) click to toggle source

Writes the serialized object’s plist to the specified filename.

    # File lib/plist/generator.rb, line 53
53:   def self.save_plist(obj, filename)
54:     File.open(filename, 'wb') do |f|
55:       f.write(obj.to_plist)
56:     end
57:   end

Private Class Methods

comment(content) click to toggle source

(Not documented)

     # File lib/plist/generator.rb, line 120
120:   def self.comment(content)
121:     return "<!-- #{content} -->\n"
122:   end
element_type(item) click to toggle source

(Not documented)

     # File lib/plist/generator.rb, line 157
157:   def self.element_type(item)
158:     case item
159:     when String, Symbol
160:       'string'
161: 
162:     when Fixnum, Bignum, Integer
163:       'integer'
164: 
165:     when Float
166:       'real'
167: 
168:     else
169:       raise "Don't know about this data type... something must be wrong!"
170:     end
171:   end
plist_node(element) click to toggle source

(Not documented)

     # File lib/plist/generator.rb, line 60
 60:   def self.plist_node(element)
 61:     output = ''
 62: 
 63:     if element.respond_to? :to_plist_node
 64:       output << element.to_plist_node
 65:     else
 66:       case element
 67:       when Array
 68:         if element.empty?
 69:           output << "<array/>\n"
 70:         else
 71:           output << tag('array') {
 72:             element.collect {|e| plist_node(e)}
 73:           }
 74:         end
 75:       when Hash
 76:         if element.empty?
 77:           output << "<dict/>\n"
 78:         else
 79:           inner_tags = []
 80: 
 81:           element.keys.sort.each do |k|
 82:             v = element[k]
 83:             inner_tags << tag('key', CGI::escapeHTML(k.to_s))
 84:             inner_tags << plist_node(v)
 85:           end
 86: 
 87:           output << tag('dict') {
 88:             inner_tags
 89:           }
 90:         end
 91:       when true, false
 92:         output << "<#{element}/>\n"
 93:       when Time
 94:         output << tag('date', element.utc.strftime('%Y-%m-%dT%H:%M:%SZ'))
 95:       when Date # also catches DateTime
 96:         output << tag('date', element.strftime('%Y-%m-%dT%H:%M:%SZ'))
 97:       when String, Symbol, Fixnum, Bignum, Integer, Float
 98:         output << tag(element_type(element), CGI::escapeHTML(element.to_s))
 99:       when IO, StringIO
100:         element.rewind
101:         contents = element.read
102:         # note that apple plists are wrapped at a different length then
103:         # what ruby's base64 wraps by default.
104:         # I used #encode64 instead of #b64encode (which allows a length arg)
105:         # because b64encode is b0rked and ignores the length arg.
106:         data = "\n"
107:         Base64::encode64(contents).gsub(/\s+/, '').scan(/.{1,68}/o) { data << $& << "\n" }
108:         output << tag('data', data)
109:       else
110:         output << comment( 'The <data> element below contains a Ruby object which has been serialized with Marshal.dump.' )
111:         data = "\n"
112:         Base64::encode64(Marshal.dump(element)).gsub(/\s+/, '').scan(/.{1,68}/o) { data << $& << "\n" }
113:         output << tag('data', data )
114:       end
115:     end
116: 
117:     return output
118:   end
tag(type, contents = '', &block) click to toggle source

(Not documented)

     # File lib/plist/generator.rb, line 124
124:   def self.tag(type, contents = '', &block)
125:     out = nil
126: 
127:     if block_given?
128:       out = IndentedString.new
129:       out << "<#{type}>"
130:       out.raise_indent
131: 
132:       out << block.call
133: 
134:       out.lower_indent
135:       out << "</#{type}>"
136:     else
137:       out = "<#{type}>#{contents.to_s}</#{type}>\n"
138:     end
139: 
140:     return out.to_s
141:   end
wrap(contents) click to toggle source

(Not documented)

     # File lib/plist/generator.rb, line 143
143:   def self.wrap(contents)
144:     output = ''
145: 
146:     output << '<?xml version="1.0" encoding="UTF-8"?>' + "\n"
147:     output << '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' + "\n"
148:     output << '<plist version="1.0">' + "\n"
149: 
150:     output << contents
151: 
152:     output << '</plist>' + "\n"
153: 
154:     return output
155:   end

Public Instance Methods

save_plist(filename) click to toggle source

Helper method for injecting into classes. Calls Plist::Emit.save_plist with self.

    # File lib/plist/generator.rb, line 32
32:   def save_plist(filename)
33:     Plist::Emit.save_plist(self, filename)
34:   end
to_plist(envelope = true) click to toggle source

Helper method for injecting into classes. Calls Plist::Emit.dump with self.

    # File lib/plist/generator.rb, line 27
27:   def to_plist(envelope = true)
28:     return Plist::Emit.dump(self, envelope)
29:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.