class Tiss::XmlCreator

Attributes

hash[R]
version[R]

Public Class Methods

[](version) click to toggle source
# File lib/tiss/xml_creator.rb, line 3
def self.[](version)
  XmlCreator.new(version)
end
new(version) click to toggle source
# File lib/tiss/xml_creator.rb, line 10
def initialize(version)
  @version = version.gsub('.', '_')
  @hash = ''
end

Public Instance Methods

build(object) click to toggle source
# File lib/tiss/xml_creator.rb, line 15
def build(object)
  Nokogiri::XML::Builder.new(encoding: 'ISO-8859-1') do |xml|
    xml['ans'].send(:mensagemTISS, 'xmlns:ans' => 'http://www.ans.gov.br/padroes/tiss/schemas') do
      populate_object(xml, object, version)
    end
  end
end
populate_object(xml, object, version) click to toggle source
# File lib/tiss/xml_creator.rb, line 23
def populate_object(xml, object, version)
  attributes = object.attributes_by(version)
  attributes.each_key do |attribute|
    value = attributes[attribute]
    next unless value.present?

    if value.is_a? Tiss::Model::Base
      xml.send(attribute) do
        populate_object(xml, value, version)
      end
    elsif value.is_a?(Array)
      # xml.send(attribute) do
        value.each do |array_value|
          xml.send(attribute) do
            populate_object(xml, array_value, version)
          end
        end
      # end
    else

      if attribute.to_s === 'hash'
        attribute = 'hash_'.to_sym
      end

      xml.send(attribute, value)
    end
  end
end