class Hermod::XmlSection

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

def self.build(options = {}, &block)

Returns the new Class

Public: builds a new class using the XmlSectionBuilder DSL
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

def self.formats

Returns a Hash

for converting their values to strings HMRC will accept.
hash by default. These formats are used by the date and monetary nodes
Internal: provides access to the formats hash, falling back on an empty
def self.formats
  @formats ||= {
    date: "%Y-%m-%d",
    datetime: "%Y-%m-%d %H:%M:%S",
    money: "%.2f",
  }
end

def self.xml_name

Returns a String

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

def format_for(type)

Raises a KeyError if the requested format is not found
Returns a format String

key.
Private: a convenience method for getting the format string for a given
def format_for(type)
  self.class.formats.fetch(type)
end

def initialize(attributes={}, &block)

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

NODE_ORDER.
directly, instead the subclasses call it as they define a useful
Internal: creates an XmlSection. This shouldn't normally be called
def initialize(attributes={}, &block)
  @attributes = attributes
  yield self if block_given?
end

def nodes

Returns a Hash

set on this node in the order they are set.
method called to set it) and the value being an array of all the values
created with the key being the name of the node (which is the name of the
unspecified key is an empty array. This stores the nodes as they are
Internal: provides access to the hash of nodes where the default for an
def nodes
  @nodes ||= Hash.new { |h, k| h[k] = [] }
end

def to_xml

Returns an XML::Node

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