class Hermod::XmlSection

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

Attributes

formats[W]
node_order[RW]
xml_name[W]
attributes[R]

Public Class Methods

build(options = {}, &block) click to toggle source

Public: builds a new class using the XmlSectionBuilder DSL

Returns the new Class

# 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
formats() click to toggle source

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

# File lib/hermod/xml_section.rb, line 79
def self.formats
  @formats ||= {
    date: "%Y-%m-%d",
    datetime: "%Y-%m-%d %H:%M:%S",
    money: "%.2f",
  }
end
new(attributes={}) { |self| ... } click to toggle source

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.
# File lib/hermod/xml_section.rb, line 55
def initialize(attributes={}, &block)
  @attributes = attributes
  yield self if block_given?
end
xml_name() click to toggle source

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

# File lib/hermod/xml_section.rb, line 70
def self.xml_name
  @xml_name ||= name.demodulize
end

Public Instance Methods

nodes() click to toggle source

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

# File lib/hermod/xml_section.rb, line 94
def nodes
  @nodes ||= Hash.new { |h, k| h[k] = [] }
end
to_xml() click to toggle source

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

# 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

Private Instance Methods

format_for(type) click to toggle source

Private: a convenience method for getting the format string for a given key.

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

# File lib/hermod/xml_section.rb, line 105
def format_for(type)
  self.class.formats.fetch(type)
end