Class: Hermod::XmlSection

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

Overview

A representation of a section of XML sent to HMRC using the Government Gateway

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) {|_self| ... } ⇒ XmlSection

Internal: creates an XmlSection. This shouldn’t normally be called directly, instead the subclasses call it as they define a useful NODE_ORDER.

name - a Symbol that corresponds to the node name in NODE_ORDER block - a Block that will be executed in the context of this class for

setting up descendents.

Yields:

  • (_self)

Yield Parameters:



55
56
57
58
# File 'lib/hermod/xml_section.rb', line 55

def initialize(attributes={}, &block)
  @attributes = attributes
  yield self if block_given?
end

Class Attribute Details

.formatsObject

Internal: provides access to the formats hash, falling back on an empty hash by default. These formats are used by the date and monetary nodes for converting their values to strings HMRC will accept.

Returns a Hash



79
80
81
82
83
84
# File 'lib/hermod/xml_section.rb', line 79

def self.formats
  @formats ||= {
    date: "%Y-%m-%d",
    money: "%.2f"
  }
end

.node_orderObject

Returns the value of attribute node_order.



62
63
64
# File 'lib/hermod/xml_section.rb', line 62

def node_order
  @node_order
end

.xml_nameObject

Internal: a class method for getting the name of the XML node used when converting instances to XML for HMRC. If the ‘xml_name` has been set then it will be used, otherwise the class name will be used as a default.

Returns a String



70
71
72
# File 'lib/hermod/xml_section.rb', line 70

def self.xml_name
  @xml_name || name.demodulize
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



23
24
25
# File 'lib/hermod/xml_section.rb', line 23

def attributes
  @attributes
end

Class Method Details

.build(options = {}, &block) ⇒ Object

Public: builds a new class using the XmlSectionBuilder DSL

Returns the new Class



14
15
16
17
18
19
20
21
# File 'lib/hermod/xml_section.rb', line 14

def self.build(options = {}, &block)
  Class.new(XmlSection).tap do |new_class|
    options.each do |name, value|
      new_class.public_send "#{name}=", value
    end
    XmlSectionBuilder.new(new_class).build(&block)
  end
end

Instance Method Details

#nodesObject

Internal: provides access to the hash of nodes where the default for an unspecified key is an empty array. This stores the nodes as they are created with the key being the name of the node (which is the name of the method called to set it) and the value being an array of all the values set on this node in the order they are set.

Returns a Hash



93
94
95
# File 'lib/hermod/xml_section.rb', line 93

def nodes
  @nodes ||= Hash.new { |h, k| h[k] = [] }
end

#to_xmlObject

Public: turns the XmlSection into an XML::Node instance (from libxml-ruby). This creates this as a node, adds any attributes (after sanitising them according to HMRC’s rules) and then adds child nodes in the order they were defined in the DSL. Nodes that have been called multiple times are added in the order they were called.

Returns an XML::Node



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hermod/xml_section.rb', line 32

def to_xml
  XML::Node.new(self.class.xml_name).tap do |root_node|
    # Add attributes
    attributes.each do |attribute_name, attribute_value|
      sane_value = sanitise_attribute(attribute_value)
      root_node[attribute_name] = sane_value if sane_value.present?
    end
    # Add child nodes
    self.class.node_order.each do |node_name|
      nodes[node_name].each do |node|
        root_node << node.to_xml
      end
    end
  end
end