Class: PropertyList::XmlGenerator
- Inherits:
-
Object
- Object
- PropertyList::XmlGenerator
- Defined in:
- lib/property-list/xml_generator.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
- #generate(obj) ⇒ Object
-
#initialize(base64_width: 68, base64_indent: true, indent_unit: "\t", initial_indent: '') ⇒ XmlGenerator
constructor
A new instance of XmlGenerator.
Constructor Details
#initialize(base64_width: 68, base64_indent: true, indent_unit: "\t", initial_indent: '') ⇒ XmlGenerator
Returns a new instance of XmlGenerator.
36 37 38 39 40 41 42 43 44 |
# File 'lib/property-list/xml_generator.rb', line 36 def initialize base64_width: 68, base64_indent: true, indent_unit: "\t", initial_indent: '' @indent_unit = indent_unit @indent_level = 0 @initial_indent = initial_indent @indent = @initial_indent + @indent_unit * @indent_level @base64_bytes_per_line = (base64_width * 6) / 8 @base64_indent = base64_indent @output = [] end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
45 46 47 |
# File 'lib/property-list/xml_generator.rb', line 45 def output @output end |
Instance Method Details
#generate(obj) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/property-list/xml_generator.rb', line 47 def generate obj if obj.respond_to? :to_plist_xml xml = obj.to_plist_xml if !xml.start_with?(@indent) xml = @indent + xml end if !xml.end_with?("\n") xml += "\n" end @output << xml return end case obj when Array if obj.empty? empty_tag 'array' else tag 'array' do obj.each {|e| generate e } end end when Hash if obj.empty? empty_tag 'dict' else tag 'dict' do obj.keys.sort_by(&:to_s).each do |k| v = obj[k] tag 'key', escape_string(k.to_s) generate v end end end when true, false empty_tag obj when Time tag 'date', obj.utc.strftime('%Y-%m-%dT%H:%M:%SZ') when Date # also catches DateTime tag 'date', obj.strftime('%Y-%m-%dT%H:%M:%SZ') when String tag 'string', escape_string(obj) when Symbol tag 'string', escape_string(obj.to_s) when Float if obj.to_i == obj tag 'real', obj.to_i else tag 'real', obj end when Integer tag 'integer', obj when IO, StringIO obj.rewind contents = obj.read data_tag contents else raise "Unsupported class: #{obj.class}" end end |