Module: XmlMethods

Instance Method Summary collapse

Instance Method Details

#to_docObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/howcast/client/utils.rb', line 16

def to_doc
  doc  = Nokogiri::XML::Document.new
  node = Nokogiri::XML::Node.new(self.class.to_s.sub(/([a-z]+::)+/i, '').downcase, doc)
  root = doc.add_child node
  self.class.attr_accessors.each do |attr|
    node  = Nokogiri::XML::Node.new("#{attr}", doc)
    value = self.send(attr)
    if value.instance_of? String
      value = inflect(attr, value) if respond_to? :inflect
      if value.instance_of? String
        node.content = value
      else
        node.add_child value
      end
      root.add_child node
    elsif value.respond_to? :to_doc
      node = value.to_doc.root
      root.add_child node
    elsif value.instance_of? Array
      value.each do |v|
        if v.respond_to? :to_doc
          node.add_child v.to_doc.root
        else
          child = Nokogiri::XML::Node.new("#{attr}".singularize, doc)
          child.content = v.to_s
          node.add_child child
        end
      end
      root.add_child node
    end
  end
  doc
end

#to_xmlObject



50
51
52
# File 'lib/howcast/client/utils.rb', line 50

def to_xml
  to_doc.to_xml
end