Class: XMLBuilder
- Inherits:
-
Object
- Object
- XMLBuilder
- Defined in:
- lib/xmlbuilder.rb
Overview
XMLBuilder is a class that allows you to easily create XML. Here’s an example:
xml = XMLBuilder.new
xml.document :type => 'xml', :use => 'example' do
xml.description "This is an example of using XMLBuilder.\n" # If you pass in a string, it will automatically input it into
xml.nextmeeting :date => Time.now+100000 do # the output string. You can't use a block with it, though.
xml.agenda "Nothing of importance will be decided.\n"
xml.clearance true, :level => :classified # Passing true in as the first parameter will cause it to be a standalone tag.
end
xml.add "I hope that this has been a good example."
end
p xml.str
<document type="xml" use="example">
<description>
This is an example of using XMLBuilder.
</description>
<nextmeeting date="2017-02-10 21:56:56 -0800">
<agenda>
Nothing of importance will be decided.
</agenda>
<clearance level="classified" />
</nextmeeting>
I hope that this has been a good example.
</document>
Instance Attribute Summary collapse
-
#str ⇒ Object
(also: #to_s, #to_str, #inspect)
readonly
Returns the value of attribute str.
Instance Method Summary collapse
-
#add(str) ⇒ Object
#add adds a string (with no processing) to the object’s string.
-
#clear ⇒ Object
#clear does the same thing as #initialize (by delegating to it).
-
#initialize ⇒ XMLBuilder
constructor
#initialize simply sets the string to “”.
-
#method_missing(name, *args, &block) ⇒ Object
(also: #add_element)
methods such as #send and #method_missing.
- #to_ary ⇒ Object
Constructor Details
#initialize ⇒ XMLBuilder
#initialize simply sets the string to “”.
28 29 30 |
# File 'lib/xmlbuilder.rb', line 28 def initialize @str = "" end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object Also known as: add_element
methods such as #send and #method_missing.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/xmlbuilder.rb', line 47 def method_missing(name, *args, &block) internal = nil if args.length == 2 if args[0].is_a? String one_tag, internal, hash = false, *args else one_tag, hash = *args end elsif args.length == 1 if args[0].is_a? Hash one_tag, hash = *[false, args[0]] elsif args[0].is_a? String one_tag, internal, hash = false, args[0], {} else one_tag, hash = *[args[0], {}] end else one_tag, hash = false, {} end @str << "<#{name}" if one_tag hash.each do |k, v| @str << " #{k}=\"#{v}\"" end @str << " />\n" else hash.each do |k, v| @str << " #{k}=\"#{v}\"" end @str << ">\n" if !internal.nil? @str << internal.to_str + "\n" elsif block block.call end @str << "</#{name}>\n" end return @str end |
Instance Attribute Details
#str ⇒ Object (readonly) Also known as: to_s, to_str, inspect
Returns the value of attribute str.
26 27 28 |
# File 'lib/xmlbuilder.rb', line 26 def str @str end |
Instance Method Details
#add(str) ⇒ Object
#add adds a string (with no processing) to the object’s string.
36 37 38 |
# File 'lib/xmlbuilder.rb', line 36 def add(str) @str << str end |
#clear ⇒ Object
#clear does the same thing as #initialize (by delegating to it).
32 33 34 |
# File 'lib/xmlbuilder.rb', line 32 def clear initialize # That's essentially what it does. end |
#to_ary ⇒ Object
39 40 41 |
# File 'lib/xmlbuilder.rb', line 39 def to_ary return [@str] end |