Class: Saxerator::Builder::XmlBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/saxerator/builder/xml_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, name, attributes) ⇒ XmlBuilder

Returns a new instance of XmlBuilder.



8
9
10
11
12
13
14
# File 'lib/saxerator/builder/xml_builder.rb', line 8

def initialize(config, name, attributes)
  @config = config
  @name = name
  @attributes = attributes
  @children = []
  @text = false
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/saxerator/builder/xml_builder.rb', line 6

def name
  @name
end

Instance Method Details

#add_node(node) ⇒ Object



16
17
18
19
# File 'lib/saxerator/builder/xml_builder.rb', line 16

def add_node(node)
  @text = true if node.is_a? String
  @children << node
end

#block_variableObject



32
33
34
35
36
37
# File 'lib/saxerator/builder/xml_builder.rb', line 32

def block_variable
  builder = REXML::Document.new
  builder << REXML::XMLDecl.new('1.0', 'UTF-8')
  to_xml(builder)
  builder
end

#to_xml(builder) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/saxerator/builder/xml_builder.rb', line 21

def to_xml(builder)
  element = REXML::Element.new(name, nil, attribute_quote: :quote)
  element.add_attributes(@attributes)
  if @text
    element.add_text(@children.join)
  else
    @children.each { |child| child.to_xml(element) }
  end
  builder.elements << element
end