Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added to_xml_value support for Rails' TimeWithZone, and made pretty p…

…rinting optional because it inserts newlines inside text contents (can invalidate e.g. atom documents).
  • Loading branch information...
commit b60fe29d732b9c681992564ab68cc7145a2274eb 1 parent f9fb74b
@Soleone Soleone authored committed
Showing with 22 additions and 3 deletions.
  1. +11 −2 lib/xml_node.rb
  2. +11 −1 test/test_generating.rb
View
13 lib/xml_node.rb
@@ -48,6 +48,15 @@ def to_xml_element
end
end
+module ActiveSupport
+ class TimeWithZone
+ def to_xml_value
+ self.xmlschema
+ end
+ end
+end
+
+
class XmlNode
attr_accessor :child_nodes
attr_reader :element
@@ -210,12 +219,12 @@ def to_s
# Use to get pretty formatted xml including DECL
# instructions
- def to_xml
+ def to_xml(pretty_print = true)
xml = []
document = REXML::Document.new
document << REXML::XMLDecl.new('1.0')
document << @element
- document.write( xml, 0)
+ document.write(xml, pretty_print ? 0 : -1)
xml.join
end
View
12 test/test_generating.rb
@@ -95,6 +95,16 @@ def test_dont_choke_on_nil_pushing
assert_equal '<feed/>', feed.to_s
end
-
+ def test_to_xml_pretty_printing
+ node = XmlNode.new('feed'){|feed| feed << XmlNode.new('text', 'Hello world') }
+
+ assert_equal "<?xml version='1.0'?>\n<feed>\n<text>\nHello world\n</text>\n</feed>", node.to_xml
+ end
+
+ def test_to_xml_no_pretty_printing
+ node = XmlNode.new('feed'){|feed| feed << XmlNode.new('text', 'Hello world') }
+
+ assert_equal "<?xml version='1.0'?><feed><text>Hello world</text></feed>", node.to_xml(pretty_printing = false)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.