Class: TaskJuggler::XMLDocument
- Defined in:
- lib/taskjuggler/XMLDocument.rb
Overview
This class provides a rather simple XML document generator. It provides basic features to create a tree of XMLElements and to generate a XML String or file. It’s much less powerful than REXML but provides a more efficient API to create XMLDocuments with lots of attributes.
Direct Known Subclasses
Instance Method Summary collapse
-
#<<(arg) ⇒ Object
Add a top-level XMLElement.
-
#initialize(&block) ⇒ XMLDocument
constructor
Create an empty XML document.
-
#to_s ⇒ Object
Produce the XMLDocument as String.
-
#write(filename) ⇒ Object
Write the XMLDocument to the specified file.
Constructor Details
#initialize(&block) ⇒ XMLDocument
Create an empty XML document.
25 26 27 |
# File 'lib/taskjuggler/XMLDocument.rb', line 25 def initialize(&block) @elements = block ? yield(block) : [] end |
Instance Method Details
#<<(arg) ⇒ Object
Add a top-level XMLElement.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/taskjuggler/XMLDocument.rb', line 30 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 |
#to_s ⇒ Object
Produce the XMLDocument as String.
44 45 46 47 48 49 50 51 |
# File 'lib/taskjuggler/XMLDocument.rb', line 44 def to_s str = '' @elements.each do |element| str << element.to_s(0) end str end |
#write(filename) ⇒ Object
Write the XMLDocument to the specified file.
54 55 56 57 58 59 60 |
# File 'lib/taskjuggler/XMLDocument.rb', line 54 def write(filename) f = filename == '.' ? $stdout : File.new(filename.untaint, 'w') @elements.each do |element| f.puts element.to_s(0) end f.close unless f == $stdout end |