lib/taskjuggler/XMLDocument.rb in taskjuggler-0.0.11 vs lib/taskjuggler/XMLDocument.rb in taskjuggler-0.1.0

- old
+ new

@@ -20,16 +20,25 @@ # or file. It's much less powerful than REXML but provides a more efficient # API to create XMLDocuments with lots of attributes. class XMLDocument # Create an empty XML document. - def initialize - @elements = [] + def initialize(&block) + @elements = block ? yield(block) : [] end # Add a top-level XMLElement. - def <<(element) - @elements << element + def <<(arg) + if arg.is_a?(Array) + @elements += arg.flatten + elsif arg.nil? + # do nothing + elsif arg.is_a?(XMLElement) + @elements << arg + else + raise ArgumentError, "Unsupported argument of type #{arg.class}: " + + "#{arg.inspect}" + end end # Produce the XMLDocument as String. def to_s str = ''