Sha256: 99fd49e376907724a8f9e8fc74562df8437a0cf08bc1260970f300701f0358ae

Contents?: true

Size: 1.3 KB

Versions: 6

Compression:

Stored size: 1.3 KB

Contents

module Quickbooks::Parser::XMLGeneration
  include Quickbooks::Parser
  include Quickbooks::Parser::XMLParsing
  include Quickbooks::Support::Inflection

  def to_qbxml
    xml_doc = Nokogiri::XML(self.class.xml_template)
    root = xml_doc.root
    log.debug "to_qbxml#nodes_size: #{root.children.size}"

    # replace all children nodes of the template with populated data nodes
    xml_nodes = []
    root.children.each do |xml_template|
      next unless xml_template.is_a? XML_ELEMENT
      attr_name = underscore(xml_template)
      log.debug "to_qbxml#attr_name: #{attr_name}"

      val = self.send(attr_name)
      next unless val && val.not_blank?

      xml_nodes += build_qbxml_nodes(xml_template, val)
      log.debug "to_qbxml#val: #{val}"
    end

    log.debug "to_qbxml#xml_nodes_size: #{xml_nodes.size}"
    root.children = xml_nodes.join('')
    set_xml_attributes!(root)
    root.to_s
  end

private

  def build_qbxml_nodes(node, val)
    val = [val].flatten
    val.map do |v|
      case v
      when QbxmlBase
        v.to_qbxml
      else
        n = node.clone
        n.children = v.to_s
        n.to_s
      end
    end
  end

  def set_xml_attributes!(node)
    node.attributes.each { |name, value| node.remove_attribute(name) }
    self.xml_attributes.each { |a,v| node.set_attribute(a, v) }
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
quickbooks_api-0.1.7 lib/quickbooks/parser/xml_generation.rb
quickbooks_api-0.1.6 lib/quickbooks/parser/xml_generation.rb
quickbooks_api-0.1.5 lib/quickbooks/parser/xml_generation.rb
quickbooks_api-0.1.4 lib/quickbooks/parser/xml_generation.rb
quickbooks_api-0.1.3 lib/quickbooks/parser/xml_generation.rb
quickbooks_api-0.1.2 lib/quickbooks/parser/xml_generation.rb