Rails XML builder without beautiful printing (i.e., Mini XML)

I use Builder :: XmlMarkup to create XML data structures for a RESTful API server.

Recently, I found an error where the pretty-printed version from Builder :: XmlMarkup created an element filled with white space instead of an empty element, as it should be.

For example, this code:

xml.outertag do
  xml.list do
    # Some code which loops through a list
  end
end

produces:

<outertag>
  <list>
  </list>
</outertag>

When the internal list is an empty list, the element must be empty - <list / "> or <list> </list>. However, the actual XML is the <list> tag, filled with a new line and other spaces.

, Builder? Builder:: XmlMarkup, : indent; after_filter.

+3
2

Builder::XmlMarkup.new - .

xml = Builder::XmlMarkup.new
xml.outertag do
  xml.list do
    # Some code which loops through a list
  end
end

xml # => <outertag><list></list></outertag>
+3

, , . , - API, XML URL-, -, .

:

  • ( ) , ActionView:: TemplateHandlers:: Builder, 0 ( ). , ActionView:: TemplateHandlers:: Builder - , , Ruby, eval(). , :

    module MinimalXml
      module Builder
        def self.included(base)
          base.class_eval do
            def compile(template)
              indent = 0
              "_set_controller_content_type(Mime::XML);" +
                "xml = ::Builder::XmlMarkup.new(:indent => #{indent});" +
                "self.output_buffer = xml.target!;" +
                template.source +
                ";xml.target!;"
            end
          end
        end
      end
    end
    
  • (, .) , - XML XML, , . Rack. , , XML ( , ..), , , .

0

Source: https://habr.com/ru/post/1715768/


All Articles