Class: Hermod::XmlSection
- Inherits:
-
Object
- Object
- Hermod::XmlSection
- 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
-
.formats ⇒ Object
Internal: provides access to the formats hash, falling back on an empty hash by default.
-
.node_order ⇒ Object
Returns the value of attribute node_order.
-
.xml_name ⇒ Object
Internal: a class method for getting the name of the XML node used when converting instances to XML for HMRC.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
-
.build(options = {}, &block) ⇒ Object
Public: builds a new class using the XmlSectionBuilder DSL.
Instance Method Summary collapse
-
#initialize(attributes = {}) {|_self| ... } ⇒ XmlSection
constructor
Internal: creates an XmlSection.
-
#nodes ⇒ Object
Internal: provides access to the hash of nodes where the default for an unspecified key is an empty array.
-
#to_xml ⇒ Object
Public: turns the XmlSection into an XML::Node instance (from libxml-ruby).
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.
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
.formats ⇒ Object
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 85 |
# 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 |
.node_order ⇒ Object
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_name ⇒ Object
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
#attributes ⇒ Object (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( = {}, &block) Class.new(XmlSection).tap do |new_class| .each do |name, value| new_class.public_send "#{name}=", value end XmlSectionBuilder.new(new_class).build(&block) end end |
Instance Method Details
#nodes ⇒ Object
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
94 95 96 |
# File 'lib/hermod/xml_section.rb', line 94 def nodes @nodes ||= Hash.new { |h, k| h[k] = [] } end |
#to_xml ⇒ Object
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 |