Sha256: 6798f20f312f3292710f1edc94e7de3ce5ddf168474fff698ef0a23db5b6378d

Contents?: true

Size: 1.78 KB

Versions: 27

Compression:

Stored size: 1.78 KB

Contents

require 'hermod/sanitisation'

module Hermod
  # A representation of an XML node with content and attributes.
  class XmlNode
    include Sanitisation

    attr_reader :name, :value, :attributes

    # 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.
    def initialize(name, value, attributes={})
      @name = name
      @value = value
      @attributes = attributes
    end

    # 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.
    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

    # 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)
    def rename_attributes(lookup_hash)
      attributes.keys.each do |attribute|
        attributes[lookup_hash.fetch(attribute)] = sanitise_attribute(attributes.delete(attribute))
      end
      self
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
hermod-3.3.0 lib/hermod/xml_node.rb
hermod-3.2.0 lib/hermod/xml_node.rb
hermod-3.0.0 lib/hermod/xml_node.rb
hermod-2.7.0 lib/hermod/xml_node.rb
hermod-2.7.0.pre.rc.1 lib/hermod/xml_node.rb
hermod-2.6.2 lib/hermod/xml_node.rb
hermod-2.6.1 lib/hermod/xml_node.rb
hermod-2.5.3 lib/hermod/xml_node.rb
hermod-2.5.2 lib/hermod/xml_node.rb
hermod-2.5.1 lib/hermod/xml_node.rb
hermod-2.5.0 lib/hermod/xml_node.rb
hermod-2.4.1 lib/hermod/xml_node.rb
hermod-2.4.0 lib/hermod/xml_node.rb
hermod-2.2.0 lib/hermod/xml_node.rb
hermod-2.1.0 lib/hermod/xml_node.rb
hermod-1.2.9 lib/hermod/xml_node.rb
hermod-1.2.8 lib/hermod/xml_node.rb
hermod-1.2.7 lib/hermod/xml_node.rb
hermod-1.2.6 lib/hermod/xml_node.rb
hermod-1.2.5 lib/hermod/xml_node.rb