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