Class: Plist::Emit::PlistBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/plist/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(indent_str) ⇒ PlistBuilder

Returns a new instance of PlistBuilder.



64
65
66
# File 'lib/plist/generator.rb', line 64

def initialize(indent_str)
  @indent_str = indent_str.to_s
end

Instance Method Details

#build(element, level = 0) ⇒ Object



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
107
108
109
# File 'lib/plist/generator.rb', line 68

def build(element, level=0)
  if element.respond_to? :to_plist_node
    element.to_plist_node
  else
    case element
    when Array
      if element.empty?
        tag('array', nil, level)
      else
        tag('array', nil, level) {
          element.collect {|e| build(e, level + 1) }.join
        }
      end
    when Hash
      if element.empty?
        tag('dict', nil, level)
      else
        tag('dict', '', level) do
          element.sort_by{|k,v| k.to_s }.collect do |k,v| 
            tag('key', CGI.escapeHTML(k.to_s), level + 1) +
            build(v, level + 1)
          end.join
        end
      end
    when true, false
      tag(element, nil, level)
    when Time
      tag('date', element.utc.strftime('%Y-%m-%dT%H:%M:%SZ'), level)
    when Date # also catches DateTime
      tag('date', element.strftime('%Y-%m-%dT%H:%M:%SZ'), level)
    when String, Symbol, Integer, Float
      tag(element_type(element), CGI.escapeHTML(element.to_s), level)
    when IO, StringIO
      data = element.tap(&:rewind).read 
      data_tag(data, level)
    else
      data = Marshal.dump(element)
      comment_tag('The <data> element below contains a Ruby object which has been serialized with Marshal.dump.') +
      data_tag(data, level)
    end
  end
end