Class: AdfBuilder::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/adf_builder/serializer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_node) ⇒ Serializer

Returns a new instance of Serializer.



13
14
15
# File 'lib/adf_builder/serializer.rb', line 13

def initialize(root_node)
  @root_node = root_node
end

Class Method Details

.to_xml(root_node) ⇒ Object



8
9
10
# File 'lib/adf_builder/serializer.rb', line 8

def to_xml(root_node)
  new(root_node).to_xml
end

Instance Method Details

#to_xmlObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/adf_builder/serializer.rb', line 17

def to_xml
  # Validate the entire tree before serializing to ensure data integrity
  @root_node.validate!

  doc = Ox::Document.new(version: "1.0")

  # XML Instruction
  instruct = Ox::Instruct.new(:xml)
  instruct[:version] = "1.0"
  doc << instruct

  # ADF Instruction
  adf_instruct = Ox::Instruct.new("ADF")
  adf_instruct[:version] = "1.0"
  doc << adf_instruct

  # ADF Root Element
  adf = Ox::Element.new("adf")
  doc << adf

  @root_node.children.each do |child|
    serialize_node(child, adf)
  end

  Ox.dump(doc)
end