lib/ebay_trader/xml_builder.rb in ebay-trader-0.9.7 vs lib/ebay_trader/xml_builder.rb in ebay-trader-0.9.8

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + module EbayTrader class XMLBuilder attr_reader :context, :xml, :depth, :tab_width @@ -23,11 +25,11 @@ def respond_to_missing?(method_name, include_private = false) super || method_name.to_s =~ /^[a-z0-9]+$/i end # Begin creating an XML string by specifying the root node. - # This also set the context scope, allowing methods and variable + # This also sets the context scope, allowing methods and variables # outside the block to be accessed. # @param [String] name the name of the root node element. # @param [Array] args the data for this element. # @param [Block] block an optional block of sub-elements to be nested # within the root node. @@ -51,18 +53,19 @@ # @param [Block] block An optional block which will further nest XML def node(name, args, &block) content = get_node_content(args) options = format_node_attributes(get_node_attributes(args)) - @xml << "#{indent_new_line}<#{name}#{options}>#{content}" + @_segments ||= [] + @_segments << "#{indent_new_line}<#{name}#{options}>#{content}" if block_given? @depth += 1 instance_eval(&block) @depth -= 1 - @xml << indent_new_line + @_segments << indent_new_line end - @xml << "</#{name}>" - @xml.strip + @_segments << "</#{name}>" + @xml = @_segments.join('').strip end # Return the first Hash in the list of arguments to #node # as this defines the attributes for the XML node. # @return [Hash] the hash of attributes for this node.