Class: Philiprehberger::XmlBuilder::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/xml_builder/node.rb

Overview

Represents a single XML element with optional attributes and children.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = {}) ⇒ Node

Returns a new instance of Node.

Parameters:

  • name (String, Symbol)

    the element tag name

  • attributes (Hash) (defaults to: {})

    element attributes



11
12
13
14
15
# File 'lib/philiprehberger/xml_builder/node.rb', line 11

def initialize(name, attributes = {})
  @name = name.to_s
  @attributes = attributes
  @children = []
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/philiprehberger/xml_builder/node.rb', line 7

def attributes
  @attributes
end

#childrenObject (readonly)

Returns the value of attribute children.



7
8
9
# File 'lib/philiprehberger/xml_builder/node.rb', line 7

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/philiprehberger/xml_builder/node.rb', line 7

def name
  @name
end

Instance Method Details

#render(indent: nil, level: 0) ⇒ String

Render this node and its children as an XML string.

Parameters:

  • indent (Integer, nil) (defaults to: nil)

    number of spaces per indentation level, or nil for compact output

  • level (Integer) (defaults to: 0)

    current nesting depth (used internally)

Returns:

  • (String)

    the rendered XML



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/philiprehberger/xml_builder/node.rb', line 22

def render(indent: nil, level: 0)
  prefix = indent ? ' ' * (indent * level) : ''
  newline = indent ? "\n" : ''

  attrs = render_attributes
  tag_open = "#{prefix}<#{@name}#{attrs}"

  if @children.empty?
    "#{tag_open} />#{newline}"
  else
    parts = ["#{tag_open}>"]
    inline = !indent || @children.all?(String)

    if inline
      @children.each { |child| parts << render_child(child, indent: nil, level: 0) }
      parts << "</#{@name}>#{newline}"
    else
      parts[0] << newline
      @children.each { |child| parts << render_child(child, indent: indent, level: level + 1) }
      parts << "#{prefix}</#{@name}>#{newline}"
    end
    parts.join
  end
end