Class: XmlElementBuilder

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

Constant Summary

Constants inherited from XmlNodeBuilder

XmlNodeBuilder::XML_VERSION

Instance Attribute Summary collapse

Attributes inherited from XmlNodeBuilder

#document

Instance Method Summary collapse

Methods inherited from XmlNodeBuilder

#build_document, #xml_document_args, #xml_version

Constructor Details

#initialize(presenter:, name:, attributes: {}, content: nil, index: nil, **options) ⇒ XmlElementBuilder

Returns a new instance of XmlElementBuilder.

Parameters:

  • presenter (ProjectXmlPresenter)
  • name (String)
  • attributes (Hash) (defaults to: {})
  • content (String) (defaults to: nil)
  • index (Integer) (defaults to: nil)

    the index for the element (when there are multiple sibling nodes)

  • options (Hash)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/services/xml_element_builder.rb', line 96

def initialize(presenter:, name:, attributes: {}, content: nil, index: nil, **options)
  super(**options)

  @presenter = presenter
  @name = name

  @attributes = attributes
  @content = content

  @index = index

  @default_method_args = []
  @default_method_args << @index unless @index.nil?
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'app/services/xml_element_builder.rb', line 3

def attributes
  @attributes
end

#contentObject (readonly) Also known as: entry

Returns the value of attribute content.



3
4
5
# File 'app/services/xml_element_builder.rb', line 3

def content
  @content
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'app/services/xml_element_builder.rb', line 3

def name
  @name
end

#presenterObject (readonly)

Returns the value of attribute presenter.



3
4
5
# File 'app/services/xml_element_builder.rb', line 3

def presenter
  @presenter
end

Instance Method Details

#add_child(child) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'app/services/xml_element_builder.rb', line 79

def add_child(child)
  return if child.nil?

  # @see https://nokogiri.org/rdoc/Nokogiri/XML/Node.html#method-i-clone
  level = 1
  cloned = child.clone(level, document)
  node.add_child(cloned)

  cloned
end

#buildNokogiri::XML::Element

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity

Returns:

  • (Nokogiri::XML::Element)


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/xml_element_builder.rb', line 56

def build
  if entry.is_a?(Hash) && entry.key?(:object_method)
    build_content!
  end

  built = build_attributes
  built.content = content

  built
rescue ArgumentError => arg_error
  Rails.logger.warn("Error building XML project metadata: #{arg_error.message}")
  nil
end

#build_attributesNokogiri::XML::Element

Returns the XML element node.

Returns:

  • (Nokogiri::XML::Element)

    the XML element node



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/xml_element_builder.rb', line 17

def build_attributes
  attributes.each do |key, attr_entry|
    value = attr_entry
    if attr_entry.is_a?(Hash) && attr_entry.key?(:object_method)
      allow_empty_attr = attr_entry.fetch(:allow_empty, true)

      message = attr_entry[:object_method]
      method_args = attr_entry[:args] || []
      method_args = @default_method_args + method_args
      value = presenter.send(message, *method_args)

      raise ArgumentError, "Value for #{key} cannot be nil" if value.nil? && !allow_empty_attr
    end

    element[key] = value unless value.nil?
  end

  element
end

#build_content!String

Returns the content for the XML element.

Returns:

  • (String)

    the content for the XML element

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/xml_element_builder.rb', line 38

def build_content!
  allow_empty_content = entry.fetch(:allow_empty, true)

  message = entry[:object_method]
  method_args = entry[:args] || []
  method_args = @default_method_args + method_args
  @content = presenter.send(message, *method_args)

  raise ArgumentError, "Content for #{name} cannot be nil" if content.nil? && !allow_empty_content
  content
end

#elementNokogiri::XML::Element

Returns the XML element node.

Returns:

  • (Nokogiri::XML::Element)

    the XML element node



8
9
10
11
12
13
14
# File 'app/services/xml_element_builder.rb', line 8

def element
  @element ||= begin
                 created = @document.create_element(name)
                 @document.root = created
                 created
               end
end

#nodeNokogiri::XML::Element

Returns the XML element node.

Returns:

  • (Nokogiri::XML::Element)

    the XML element node



75
76
77
# File 'app/services/xml_element_builder.rb', line 75

def node
  @node ||= build
end