Class: XmlTreeBuilder

Inherits:
XmlNodeBuilder show all
Defined in:
app/services/xml_tree_builder.rb

Constant Summary

Constants inherited from XmlNodeBuilder

XmlNodeBuilder::XML_VERSION

Instance Attribute Summary collapse

Attributes inherited from XmlNodeBuilder

#document, #node

Instance Method Summary collapse

Methods inherited from XmlNodeBuilder

#build_document, #xml_document_args, #xml_version

Constructor Details

#initialize(children: [], **options) ⇒ XmlTreeBuilder

Returns a new instance of XmlTreeBuilder.

Parameters:

  • children (Array<XmlNodeBuilder>) (defaults to: [])

    XmlNodeBuilder objects for the child nodes in the XML tree

  • options (Hash)

    arguments for the root XmlElementBuilder



70
71
72
73
74
75
# File 'app/services/xml_tree_builder.rb', line 70

def initialize(children: [], **options)
  @root_builder_options = options.dup
  @children = parse_child_entries(children)

  super(document: document)
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



4
5
6
# File 'app/services/xml_tree_builder.rb', line 4

def children
  @children
end

#root_builder_optionsObject (readonly)

Returns the value of attribute root_builder_options.



4
5
6
# File 'app/services/xml_tree_builder.rb', line 4

def root_builder_options
  @root_builder_options
end

Instance Method Details

#buildNokogiri::XML::Element

Returns:

  • (Nokogiri::XML::Element)


21
22
23
24
25
26
27
28
# File 'app/services/xml_tree_builder.rb', line 21

def build
  nodes = children.map(&:build)
  nodes.each do |child|
    root_builder.add_child(child)
  end

  node
end

#null_builderObject



6
7
8
# File 'app/services/xml_tree_builder.rb', line 6

def null_builder
  @null_builder ||= XmlNullBuilder.new(**root_builder_options)
end

#parse_child_entries(entries) ⇒ Array<Nokogiri::XML::Node>

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

Parameters:

  • entries (Array<Hash>)

    options for constructing each child node

Returns:

  • (Array<Nokogiri::XML::Node>)

    an array of XML child nodes



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/services/xml_tree_builder.rb', line 35

def parse_child_entries(entries)
  instances = [:default]
  builders = entries.map do |child_entry|
    child_entry.values.map do |child_args|
      multiple_children = child_args[:multiple] || false

      if multiple_children
        message = child_args[:object_method]

        method_args = child_args[:args] || []
        instances = root_builder.presenter.send(message, *method_args)
      end

      instances.each_with_index.map do |_instance, i|
        builder_args = child_args.dup
        builder_args[:presenter] = root_builder.presenter
        builder_args[:document] = document

        if multiple_children
          builder_args[:index] = i
          builder_args.delete(:multiple)
        end
        builder_args.delete(:object_method)
        builder_args.delete(:args)
        self.class.new(**builder_args)
      end
    end
  end
  builders.flatten
end

#root_builderObject



10
11
12
13
14
15
16
# File 'app/services/xml_tree_builder.rb', line 10

def root_builder
  @root_builder ||= begin
                      default_builder = XmlElementBuilder.new(**root_builder_options)
                      null_builder if default_builder.blank?
                      default_builder
                    end
end