Class: Hermod::XmlNode

Inherits:
Object
  • Object
show all
Includes:
Sanitisation
Defined in:
lib/hermod/xml_node.rb

Overview

A representation of an XML node with content and attributes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, attributes = {}) ⇒ XmlNode

Internal: creates a XmlNode. This is used by the XmlSectionBuilder’s node building methods and should not be called manually.

name - the name of the node as it appears in the XML value - the node contents as a string. attributes - a Hash of attributes as Symbol -> value pairs. The symbol

must be in the list of attributes allowed for the node as
set in the builder.


18
19
20
21
22
# File 'lib/hermod/xml_node.rb', line 18

def initialize(name, value, attributes={})
  @name = name
  @value = value
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/hermod/xml_node.rb', line 8

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/hermod/xml_node.rb', line 8

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/hermod/xml_node.rb', line 8

def value
  @value
end

Instance Method Details

#rename_attributes(lookup_hash) ⇒ Object

Internal: replaces symbol attributes with strings looked up in the provided hash

lookup_hash - the hash to use to convert symbols to strings HMRC recognise

Returns self so it can be used in a call chain (This may change in future)



48
49
50
51
52
53
# File 'lib/hermod/xml_node.rb', line 48

def rename_attributes(lookup_hash)
  attributes.keys.each do |attribute|
    attributes[lookup_hash.fetch(attribute)] = sanitise_attribute(attributes.delete(attribute))
  end
  self
end

#to_xmlObject

Internal: turns the XmlNode into an XML::Node including any attributes without any sanitisation (currently - this may change in a future version).

Returns an XML::Node built from the XmlNode object.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hermod/xml_node.rb', line 29

def to_xml
  if value.respond_to? :to_xml
    value.to_xml
  else
    XML::Node.new(@name, @value).tap do |node|
      @attributes.each do |attribute_name, attribute_value|
        node[attribute_name] = attribute_value if attribute_value.present?
      end
    end
  end
end