Parent

Builder::XmlBase

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

Public Class Methods

new(indent=0, initial=0) click to toggle source

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.

    # File lib/builder/xmlbase.rb, line 22
22:     def initialize(indent=0, initial=0)
23:       @indent = indent
24:       @level  = initial
25:     end

Public Instance Methods

<<(text) click to toggle source

Append text to the output target without escaping any markup. May be used within the markup brakets 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.

    # File lib/builder/xmlbase.rb, line 97
97:     def <<(text)
98:       _text(text)
99:     end
method_missing(sym, *args, &block) click to toggle source

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.

    # File lib/builder/xmlbase.rb, line 37
37:     def method_missing(sym, *args, &block)
38:       text = nil
39:       attrs = nil
40:       sym = "#{sym}:#{args.shift}" if args.first.kind_of?(Symbol)
41:       args.each do |arg|
42:         case arg
43:         when Hash
44:           attrs ||= {}
45:           attrs.merge!(arg)
46:         else
47:           text ||= ''
48:           text << arg.to_s
49:         end
50:       end
51:       if block
52:         unless text.nil?
53:           raise ArgumentError, "XmlMarkup cannot mix a text argument with a block"
54:         end
55:         _indent
56:         _start_tag(sym, attrs)
57:         _newline
58:         _nested_structures(block)
59:         _indent
60:         _end_tag(sym)
61:         _newline
62:       elsif text.nil?
63:         _indent
64:         _start_tag(sym, attrs, true)
65:         _newline
66:       else
67:         _indent
68:         _start_tag(sym, attrs)
69:         text! text
70:         _end_tag(sym)
71:         _newline
72:       end
73:       @target
74:     end
nil?() click to toggle source

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).

     # File lib/builder/xmlbase.rb, line 107
107:     def nil?
108:       false
109:     end
tag!(sym, *args, &block) click to toggle source

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

    # File lib/builder/xmlbase.rb, line 30
30:     def tag!(sym, *args, &block)
31:       method_missing(sym.to_sym, *args, &block)
32:     end
text!(text) click to toggle source

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

  builder.p { |b| b.br; b.text! "HI" }   #=>  <p><br/>HI</p>
    # File lib/builder/xmlbase.rb, line 80
80:     def text!(text)
81:       _text(_escape(text))
82:     end

Private Instance Methods

_escape(text) click to toggle source
     # File lib/builder/xmlbase.rb, line 114
114:     def _escape(text)
115:       text.to_xs
116:     end
_escape_quote(text) click to toggle source
     # File lib/builder/xmlbase.rb, line 118
118:     def _escape_quote(text)
119:       _escape(text).gsub(%{"}, '&quot;')  # " WART
120:     end
_indent() click to toggle source
     # File lib/builder/xmlbase.rb, line 127
127:     def _indent
128:       return if @indent == 0 || @level == 0
129:       text!(" " * (@level * @indent))
130:     end
_nested_structures(block) click to toggle source
     # File lib/builder/xmlbase.rb, line 132
132:     def _nested_structures(block)
133:       @level += 1
134:       block.call(self)
135:     ensure
136:       @level -= 1
137:     end
_newline() click to toggle source
     # File lib/builder/xmlbase.rb, line 122
122:     def _newline
123:       return if @indent == 0
124:       text! "\n"
125:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.