Class TaliaUtil::Xml::BaseBuilder
In: lib/talia_util/xml/base_builder.rb
Parent: Object

Base class for builders that create XML representations. This uses a Builder::XmlMarkup object in the background which does the actual XML writing.

All builders will be used through the open method, which can be passed either a Builder::XmlMarkup object, or the options to create one.

Subclasses must provide a method named "build_structure" method. The method must accept a block, and should create the outer structure of the XML document and yield to the block to build the inner content.

Example:

 class ABuilder < BaseBuilder

   def build_structure
     @builder.div { yield }
   end

   def write_stuff(text)
     @builder.p { @builder.text!(text) }
   end
 end

 xml = ABuilder.make_xml_string do |builder|
    builder.write_stuff("Hello")
    builder.write_stuff("world")
 end

Which would result in:

 <div>
   <p>Hello</p>
   <p>World</p>
 </div>

Methods

make_xml_string   new   open  

Public Class methods

Builds to a string, using a default builder. This returns the string and otherwise works like open

[Source]

    # File lib/talia_util/xml/base_builder.rb, line 54
54:       def self.make_xml_string
55:         xml = ''
56:         open(:target => xml, :indent => 2) do |builder|
57:           yield(builder)
58:         end
59:         
60:         xml
61:       end

Create a new builder

[Source]

    # File lib/talia_util/xml/base_builder.rb, line 66
66:       def initialize(options)
67:         @builder = options[:builder]
68:         @builder ||= Builder::XmlMarkup.new(options)
69:       end

Creates a new builder. The options are equivalent for the options of the underlying Xml builder. The builder itself will be passed to the block that is called by this method. If you pass a :builder option instead, it will use the given builder instead of creating a new one

[Source]

    # File lib/talia_util/xml/base_builder.rb, line 45
45:       def self.open(options)
46:         my_builder = self.new(options)
47:         my_builder.send(:build_structure) do
48:           yield(my_builder)
49:         end
50:       end

[Validate]