Class

Builder::XmlBase

Inheritance
< BlankSlate < Object

XmlBase is a base class for building XML builders. See Builder::XmlMarkup and Builder::XmlEvents for examples.

Methods

Class

Visibility Signature
public new (indent=0, initial=0)

Instance

Visibility Signature
public << (text)
public method_missing (sym, *args, &block)
public nil? ()
public tag! (sym, *args, &block)
public text! (text)

Class Method Detail

new(indent=0, initial=0)

Create an XML markup builder.

out:Object receiving the markup. out must respond to <<.
indent:Number of spaces used for indentation (0 implies no indentation and no line breaks).
initial:Level of initial indentation.

Instance Method Detail

<<(text)

Append text to the output target without escaping any markup. May be used within the markup brackets as:

  builder.p { |x| x << "<br/>HI" }   #=>  <p><br/>HI</p>

This is useful when using non-builder enabled software that generates strings. Just insert the string directly into the builder without changing the inserted markup.

It is also useful for stacking builder objects. Builders only use << to append to the target, so by supporting this method/operation builders can use other builders as their targets.

method_missing(sym, *args, &block)

Create XML markup based on the name of the method. This method is never invoked directly, but is called for each markup method in the markup block.

nil?()

For some reason, nil? is sent to the XmlMarkup object. If nil? is not defined and method_missing is invoked, some strange kind of recursion happens. Since nil? won‘t ever be an XML tag, it is pretty safe to define it here. (Note: this is an example of cargo cult programming, cf. fishbowl.pastiche.org/2004/10/13/cargo_cult_programming).

tag!(sym, *args, &block)

Create a tag named sym. Other than the first argument which is the tag name, the arguments are the same as the tags implemented via method_missing.

text!(text)

Append text to the output target. Escape any markup. May be used within the markup brackets as:

  builder.p { |b| b.br; b.text! "HI" }   #=>  <p><br/>HI</p>