Sha256: 9aabf2f2cab71ddc96559d945f2bb6f572b6f8cbe5d871edd80dcf46b1f3df80

Contents?: true

Size: 1.15 KB

Versions: 10

Compression:

Stored size: 1.15 KB

Contents

module Exlibris
  module Aleph
    # 
    # Utility for parsing and building XML
    # 
    module XmlUtil
      require 'nokogiri'

      def self.included(klass)
        klass.class_eval do
          extend ClassAttributes
        end
      end

      module ClassAttributes
        def xml_options
          @xml_options ||= {
            :encoding => 'UTF-8',
            :indent => 0,
            :save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
          }
        end
      end

      attr_reader :raw_xml
      protected :raw_xml

      # Returns an XML string and takes any args that are
      # understood by Nokogiri::XML::Builder.
      def build_xml options={}, &block
        Nokogiri::XML::Builder.new(options.merge(:encoding => 'UTF-8'), &block).to_xml(xml_options).strip
      end
      protected :build_xml

      def xml_options
        @xml_options ||= self.class.xml_options
      end
      protected :xml_options

      def xml(*args)
        Nokogiri::XML((args.shift[:xml] || raw_xml))
      end
      protected :xml

      def to_xml
        xml.to_xml(xml_options).strip
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
exlibris-aleph-1.1.0 lib/exlibris/aleph/xml_util.rb
exlibris-aleph-1.0.8 lib/exlibris/aleph/xml_util.rb
exlibris-aleph-1.0.7 lib/exlibris/aleph/xml_util.rb
exlibris-aleph-1.0.6 lib/exlibris/aleph/xml_util.rb
exlibris-aleph-1.0.5 lib/exlibris/aleph/xml_util.rb
exlibris-aleph-1.0.4 lib/exlibris/aleph/xml_util.rb
exlibris-aleph-1.0.3 lib/exlibris/aleph/xml_util.rb
exlibris-aleph-1.0.2 lib/exlibris/aleph/xml_util.rb
exlibris-aleph-1.0.1 lib/exlibris/aleph/xml_util.rb
exlibris-aleph-1.0.0 lib/exlibris/aleph/xml_util.rb