Class: Tiss::XmlCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/tiss/xml_creator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ XmlCreator

Returns a new instance of XmlCreator.



10
11
12
13
# File 'lib/tiss/xml_creator.rb', line 10

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

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



8
9
10
# File 'lib/tiss/xml_creator.rb', line 8

def hash
  @hash
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/tiss/xml_creator.rb', line 7

def version
  @version
end

Class Method Details

.[](version) ⇒ Object



3
4
5
# File 'lib/tiss/xml_creator.rb', line 3

def self.[](version)
  XmlCreator.new(version)
end

Instance Method Details

#build(object) ⇒ Object



15
16
17
18
19
20
21
# 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) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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