Class: XMLBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2/amitools/xmlbuilder.rb

Defined Under Namespace

Classes: PathParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = nil) ⇒ XMLBuilder

Create a new XMLBuilder rooted at the given element, or at a new document if no root is given



45
46
47
# File 'lib/ec2/amitools/xmlbuilder.rb', line 45

def initialize(root = nil)
  @root = root || REXML::Document.new()
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



42
43
44
# File 'lib/ec2/amitools/xmlbuilder.rb', line 42

def root
  @root
end

Instance Method Details

#[](path) ⇒ Object

Retrieve a builder for the element at the given path



50
51
52
53
54
55
56
# File 'lib/ec2/amitools/xmlbuilder.rb', line 50

def [](path)
  nodes = PathParser.parse(path)
  rexml_node = nodes.inject(@root) do |rexml_node, parser_node|
    parser_node.walk_visit(rexml_node)
  end
  XMLBuilder.new(nodes.last.retrieve_visit(rexml_node))
end

#[]=(path, value) ⇒ Object

Set the value of the element or attribute at the given path



59
60
61
62
63
64
65
66
67
# File 'lib/ec2/amitools/xmlbuilder.rb', line 59

def []=(path, value)
  # Don't create elements or make assignments for nil values
  return if value.nil?
  nodes = PathParser.parse(path)
  rexml_node = nodes.inject(@root) do |rexml_node, parser_node|
    parser_node.walk_visit(rexml_node)
  end
  nodes.last.assign_visit(rexml_node, value)
end