Sha256: 371c14c80a94041e3dc9daa5dd93e4c56a92741e4934543ee6334a1b9c6a1c87

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 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.inject([]) do |a, v|
      a << case v
        when QbxmlBase
          v.to_qbxml
        else
          n = node.clone
          n.children = val.to_s
          n
        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

2 entries across 2 versions & 1 rubygems

Version Path
quickbooks_api-0.1.1 lib/quickbooks/parser/xml_generation.rb
quickbooks_api-0.1.0 lib/quickbooks/parser/xml_generation.rb