Module: Pacemaker::Xml

Included in:
Puppet::Provider::PacemakerXML, Serverspec::Type::PacemakerXML
Defined in:
lib/pacemaker/xml/xml.rb

Overview

functions that are used to generate XML documents and create XML patches

Instance Method Summary collapse

Instance Method Details

#xml_document(path = [], root = nil) ⇒ REXML::Element

create a new xml document use .root to get the document root

Parameters:

  • path (String, Array<String>) (defaults to: [])

    create this sequence of path elements

  • root (REXML::Document) (defaults to: nil)

    use existing element as a root instead of creating a new one

Returns:

  • (REXML::Element)

    element point to the last path component



9
10
11
12
13
14
15
16
17
# File 'lib/pacemaker/xml/xml.rb', line 9

def xml_document(path = [], root = nil)
  root = REXML::Document.new unless root
  element = root
  path = Array(path) unless path.is_a? Array
  path.each do |component|
    element = element.add_element component
  end
  element
end

#xml_element(tag, hash, skip_attributes = nil) ⇒ REXML::Element

convert hash to xml element

Parameters:

  • tag (String)

    what xml tag to create

  • hash (Hash)

    attributes data structure

  • skip_attributes (String, Array<String>) (defaults to: nil)

    skip these hash keys

Returns:

  • (REXML::Element)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pacemaker/xml/xml.rb', line 24

def xml_element(tag, hash, skip_attributes = nil)
  return unless hash.is_a? Hash
  element = REXML::Element.new tag.to_s
  hash.each do |attribute, value|
    attribute = attribute.to_s
    # skip attributes that were specified to be skipped
    next if skip_attributes == attribute ||
        (skip_attributes.respond_to?(:include?) && skip_attributes.include?(attribute))
    # skip array and hash values. add only scalar ones
    next if value.is_a?(Array) || value.is_a?(Hash)
    element.add_attribute attribute, value
  end
  element
end

#xml_pretty_format(element) ⇒ String

output xml element as the actual xml text with indentation

Parameters:

  • element (REXML::Element)

Returns:

  • (String)


42
43
44
45
46
47
48
49
# File 'lib/pacemaker/xml/xml.rb', line 42

def xml_pretty_format(element)
  return unless element.is_a? REXML::Element
  formatter = REXML::Formatters::Pretty.new
  formatter.compact = true
  xml = ''
  formatter.write element, xml
  xml + "\n"
end