class XMLBuilder
XMLBuilder
is a library that allows you to easily create XML.
Licensed under MIT
Written by Coderz
Attributes
inspect[R]
separator set to two spaces by default, used in nesting
separator[RW]
str[R]
separator set to two spaces by default, used in nesting
to_s[R]
separator set to two spaces by default, used in nesting
to_str[R]
separator set to two spaces by default, used in nesting
Public Class Methods
new(separator = @@default_separator)
click to toggle source
# File lib/xmlbuilder.rb, line 15 def initialize(separator = @@default_separator) @str = "" @depth = 0 @separator = separator end
Public Instance Methods
add(*strs)
click to toggle source
Adds a string (with no preprocessing) to the object's string.
# File lib/xmlbuilder.rb, line 28 def add(*strs) @str << strs.flatten.join("") end
add_element(name, *args) { || ... }
click to toggle source
Takes the name of the tag to add, an optional string to put in the tag, an optional boolean parameter which signifies whether to make it a single tag or not, any options to put in the tag, and a block to evaluate between the opening and closing tags. Aliased to method_missing
to allow dynamic tag creation.
# File lib/xmlbuilder.rb, line 33 def add_element(name, *args) one_tag, internal, attrs = process_args args # logic time add indentation, ?<, name attrs.each do |attr, value| add " #{attr}=\"#{value}\"" end if one_tag add " />\n" return self else add ?> end if internal add internal elsif block_given? @depth += 1 add "\n" yield @depth -= 1 end add indentation unless internal add "</#{name}>\n" return self end
Also aliased as: method_missing
clear()
click to toggle source
Sets the stored string to “” and the depth to 0.
# File lib/xmlbuilder.rb, line 22 def clear initialize(@separator) self end
Private Instance Methods
indentation()
click to toggle source
# File lib/xmlbuilder.rb, line 87 def indentation; @separator * @depth; end
process_args(args)
click to toggle source
# File lib/xmlbuilder.rb, line 60 def process_args(args) # Argument cheat sheet: # <name hash[0]="hash[1]"> # internal # </name> internal = nil if args.size == 2 if args[0] == !!args[0] one_tag, hash = *args else one_tag, internal, hash = false, *args end elsif args.size == 1 if args[0].is_a? Hash one_tag, hash = *[false, args[0]] elsif args[0] == !!args[0] one_tag, hash = args[0], {} else one_tag, internal, hash = false, args[0].to_s, {} end else one_tag, hash = false, {} end return one_tag, internal, hash end