Sha256: e7184b65cf358a7d64d63b1619c2d4985e58a1bef70b2fe902b33993230ed366

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

module Tiss
  class Creator
    def self.[](version)
      Creator.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

1 entries across 1 versions & 1 rubygems

Version Path
tiss-ruby-0.1.0 lib/tiss/creator.rb