module 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.

Constants

DEFAULT_INDENT

Public Class Methods

dump(obj, envelope = true, options = {}) 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 <data> 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 45
def self.dump(obj, envelope = true, options = {})
  options = { :indent => DEFAULT_INDENT }.merge(options)

  output = PlistBuilder.new(options[:indent]).build(obj)
  output = wrap(output) if envelope

  output
end
save_plist(obj, filename, options = {}) click to toggle source

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

# File lib/plist/generator.rb, line 55
def self.save_plist(obj, filename, options = {})
  File.open(filename, 'wb') do |f|
    f.write(obj.to_plist(true, options))
  end
end

Private Class Methods

wrap(contents) click to toggle source
# File lib/plist/generator.rb, line 162
def self.wrap(contents)
  output =  '<?xml version="1.0" encoding="UTF-8"?>' + "\n"
  output << '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' + "\n"
  output << '<plist version="1.0">' + "\n"
  output << contents
  output << '</plist>' + "\n"

  output
end

Public Instance Methods

save_plist(filename, options = {}) click to toggle source

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

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

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

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