Class: Factbase::ToXML

Inherits:
Object
  • Object
show all
Defined in:
lib/factbase/to_xml.rb

Overview

Factbase to XML converter.

This class helps converting an entire Factbase to XML format, for example:

require 'factbase/to_xml'
fb = Factbase.new
puts Factbase::ToXML.new(fb).xml
Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024-2026 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(fb, sorter = '_id') ⇒ ToXML

Constructor.



24
25
26
27
# File 'lib/factbase/to_xml.rb', line 24

def initialize(fb, sorter = '_id')
  @fb = fb
  @sorter = sorter
end

Instance Method Details

#xmlString

Convert the entire factbase into XML.

Returns:

  • (String)

    The factbase in XML format



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/factbase/to_xml.rb', line 31

def xml
  meta = { version: Factbase::VERSION, size: @fb.export.size }
  Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.fb(meta) do
      Factbase::Flatten.new(@fb.each.to_a, @sorter).it.each do |m|
        xml.f_ do
          m.sort.to_h.each do |k, vv|
            if vv.is_a?(Array)
              xml.__send__(:"#{k}_") do
                vv.each do |v|
                  xml.__send__(:v, to_str(v), t: type_of(v))
                end
              end
            else
              xml.__send__(:"#{k}_", to_str(vv), t: type_of(vv))
            end
          end
        end
      end
    end
  end.to_xml
end