REXMLBuilder

Build XML Documents programatically with Ruby and REXML via the Builder Pattern —made possible by Ruby’s blocks.

XmlBuilder uses REXML to contruct XML documents, helping to ensure XML specification conforamcy.

Usage

  x = REXMLBuilder.new

  favs = [ 'cake', 'jelly', 'salt' ]

  x.table( :width=>'100%' ) {
    x.tr {
      favs.each { |v| x.td v }
      }
    }
  }

You can also setup the XmlBuilder to assume an implicit self, so the explict reciever is not needed.

  x = REXMLBuilder.new( :implicit )

  x.table( :width=>'100%' ) {
    tr {
        td "1" ; td "2" ; td "3"
      }
    }
  }

Implict building is more elegant in form, but it is not as functional because it makes it more difficult to refer to external values, ie. if ‘favs’ were used as in the previous example, it would be interpreted as another tag entry, not the array.

Methods
Classes and Modules
Module REXMLBuilder::REXMLUtil
Public Class methods
new( *options )

Prepare builder.

# File lib/facets/more/rexmlbuilder.rb, line 113
  def initialize( *options )
    # initialize options
    @options = (Hash === options.last ? options.pop : {} )
    options.each { |o| @options[o] = true }
    # initialize stack
    @stack = []
    # initialize buffer
    @target = builder.new( @options )
  end
Public Instance methods
<<( str )

raw insert

# File lib/facets/more/rexmlbuilder.rb, line 76
  def <<( str )
    r = builder.raw( str )
    if Array === r
      r.each { |e| @target << e }
    else
      @target << r
    end
    self
  end
out( str=nil )

Redirection functor.

# File lib/facets/more/rexmlbuilder.rb, line 91
  def out( str=nil )
    if str
      self << str
    else
      @builder_functor ||= Functor.new( &__method__(:+) )
    end
  end
to_s()

text output

# File lib/facets/more/rexmlbuilder.rb, line 101
  def to_s
    @target.to_s
  end
to_s( indent=nil )

Output XML document.

# File lib/facets/more/rexmlbuilder.rb, line 68
  def to_s( indent=nil )
    o = ""
    @target.write( o, indent || @options[:indent] || 0 )
    o
  end
Private Instance methods
+( op, *args, &blk )

Creates object via builder module, stores as current and appends to buffer.

# File lib/facets/more/rexmlbuilder.rb, line 151
  def +( op, *args, &blk )
    obj = builder.send( op, *args, &blk )
    @current = obj
    @target << obj
    self
  end
builder()
# File lib/facets/more/rexmlbuilder.rb, line 107
  def builder
    REXMLUtil
  end
method_missing( tag, *args, &block )

Reroute — Core functionality.

# File lib/facets/more/rexmlbuilder.rb, line 125
  def method_missing( tag, *args, &block )
    tag = tag.to_s
    if tag[-1,1] == '?'
      raise ArgumentError if block_given?
      out.instruct( tag.chomp('?'), *args )
    elsif tag[-1,1] == '!'
      out.declare( tag.chomp('!'), *args )
    else
      out.element( tag, *args )
    end
    if @current and block_given?
      @stack << @target
      @target = @current
      if @options[:implicit]
        instance_eval( &block )
      else
        block.call
      end
      @target = @stack.pop
    end
    @target
  end