Sha256: bb72b678839ee0d3bdff28c3cfa74011ddc31d93fe040c2120a6412df79a0e6c

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module Tiss
  class XmlCreator
    def self.[](version)
      XmlCreator.new(version)
    end

    attr_reader :version
    attr_reader :hash

    def initialize(version)
      @version = version.gsub('.', '_')
      @hash = ''
    end

    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

    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tiss-ruby-0.2.2 lib/tiss/xml_creator.rb
tiss-ruby-0.2.1 lib/tiss/xml_creator.rb
tiss-ruby-0.2.0 lib/tiss/xml_creator.rb