Sha256: ea73e6af0d774e55687283c6edc2460b428bb5ec1e4ac70d247066024cc2ae7a

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require 'extensions/ox_extensions'

module Goldendocx
  module XmlSerializers
    module Ox
      class << self
        def parse(xml, paths = [])
          xml = ::Ox.parse(xml)
          xml = ::Ox::Document.new.tap { |document| document << xml } unless xml.is_a?(::Ox::Document)
          search(xml, paths)
        end

        def search(node, paths)
          return node if paths.blank?

          node.locate(paths.join('/'))
        end

        def build_xml(tag, &)
          xml = build_element(tag, &)
          ::Ox.dump(xml, indent: -1, with_xml: true, encoding: 'UTF-8')
        end

        def build_document_xml(tag, namespaces = [], ignore_namespaces = [], &)
          xml = build_document(tag, namespaces, ignore_namespaces, &)
          ::Ox.dump(xml, indent: -1, with_xml: true)
        end

        def build_document(tag, namespaces = [], ignore_namespaces = [])
          ::Ox::Document.new(encoding: 'UTF-8', version: '1.0', standalone: 'yes').tap do |root|
            root << ::Ox::Element.new(tag).tap do |document|
              Goldendocx::NAMESPACES.slice(*namespaces).each do |key, namespace|
                document["xmlns:#{key}"] = namespace
              end

              document['mc:Ignorable'] = ignore_namespaces.join(' ') unless ignore_namespaces.empty?

              yield(document) if block_given?
            end
          end
        end

        def build_element(tag)
          ::Ox::Element.new(tag).tap do |element|
            yield(element) if block_given?
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
goldendocx-0.3.0 lib/goldendocx/xml_serializers/ox.rb