Module: Plist::Emit
Overview
Create a plist
You can dump an object to a plist in one of two ways:
- Plist::Emit.dump(obj)
-
obj.to_plist
- This requires that you mixin the Plist::Emit module, which is already done for
ArrayandHash.
- This requires that you mixin the Plist::Emit module, which is already done for
The following Ruby classes are converted into native plist types:
Array, Bignum, Date, DateTime, Fixnum, Float, Hash, Integer, String, Symbol, Time, true, false
ArrayandHashare both recursive; their elements will be converted into plist nodes inside theand containers (respectively). IO(and its descendants) andStringIOobjects are read from and their contents placed in a element.- User classes may implement
to_plist_nodeto dictate how they should be serialized; otherwise the object will be passed to Marshal.dump and the result placed in a element.
For detailed usage instructions, refer to USAGE and the methods documented below.
Defined Under Namespace
Classes: PlistBuilder
Constant Summary collapse
- DEFAULT_INDENT =
"\t"
Class Method Summary collapse
-
.dump(obj, envelope = true, options = {}) ⇒ Object
The following Ruby classes are converted into native plist types: Array, Bignum, Date, DateTime, Fixnum, Float, Hash, Integer, String, Symbol, Time.
-
.save_plist(obj, filename, options = {}) ⇒ Object
Writes the serialized object's plist to the specified filename.
Instance Method Summary collapse
-
#save_plist(filename, options = {}) ⇒ Object
Helper method for injecting into classes.
-
#to_plist(envelope = true, options = {}) ⇒ Object
Helper method for injecting into classes.
Class Method Details
.dump(obj, envelope = true, options = {}) ⇒ Object
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.
45 46 47 48 49 50 51 52 |
# File 'lib/plist/generator.rb', line 45 def self.dump(obj, envelope = true, = {}) = { :indent => DEFAULT_INDENT }.merge() output = PlistBuilder.new([:indent]).build(obj) output = wrap(output) if envelope output end |
.save_plist(obj, filename, options = {}) ⇒ Object
Writes the serialized object's plist to the specified filename.
55 56 57 58 59 |
# File 'lib/plist/generator.rb', line 55 def self.save_plist(obj, filename, = {}) File.open(filename, 'wb') do |f| f.write(obj.to_plist(true, )) end end |
Instance Method Details
#save_plist(filename, options = {}) ⇒ Object
Helper method for injecting into classes. Calls Plist::Emit.save_plist with self.
33 34 35 |
# File 'lib/plist/generator.rb', line 33 def save_plist(filename, = {}) Plist::Emit.save_plist(self, filename, ) end |