Class: XMLBuilder
- Inherits:
-
Object
- Object
- XMLBuilder
- Defined in:
- lib/xmlbuilder.rb
Overview
XMLBuilder is a library 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(separator = " ") ⇒ XMLBuilder
constructor
#initialize simply sets the stored string to “” and the depth (used in nesting tags) to 0.
-
#method_missing(name, *args, &block) ⇒ Object
(also: #add_element)
methods such as #send and #method_missing.
- #to_ary ⇒ Object
Constructor Details
#initialize(separator = " ") ⇒ XMLBuilder
#initialize simply sets the stored string to “” and the depth (used in nesting tags) to 0.
28 29 30 31 32 |
# File 'lib/xmlbuilder.rb', line 28 def initialize(separator=" ") # separator set to two spaces by default, used in nesting @str = "" @depth = 0 @separator = separator 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.
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 86 87 88 89 90 91 |
# File 'lib/xmlbuilder.rb', line 49 def method_missing(name, *args, &block) internal = nil # Internal is a string that is put between the sides of the element 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 << @separator.to_s * @depth @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 @depth += 1 block.call @depth -= 1 end @str << @separator * @depth @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.
38 39 40 |
# File 'lib/xmlbuilder.rb', line 38 def add(str) @str << str end |
#clear ⇒ Object
#clear does the same thing as #initialize (by delegating to it).
34 35 36 |
# File 'lib/xmlbuilder.rb', line 34 def clear initialize # That's essentially what it does. end |
#to_ary ⇒ Object
41 42 43 |
# File 'lib/xmlbuilder.rb', line 41 def to_ary return [@str] end |