Class: XmlElementBuilder
- Inherits:
-
XmlNodeBuilder
- Object
- XmlNodeBuilder
- XmlElementBuilder
- Defined in:
- app/services/xml_element_builder.rb
Constant Summary
Constants inherited from XmlNodeBuilder
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#content ⇒ Object
(also: #entry)
readonly
Returns the value of attribute content.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#presenter ⇒ Object
readonly
Returns the value of attribute presenter.
Attributes inherited from XmlNodeBuilder
Instance Method Summary collapse
- #add_child(child) ⇒ Object
-
#build ⇒ Nokogiri::XML::Element
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity.
-
#build_attributes ⇒ Nokogiri::XML::Element
The XML element node.
-
#build_content! ⇒ String
The content for the XML element.
-
#element ⇒ Nokogiri::XML::Element
The XML element node.
-
#initialize(presenter:, name:, attributes: {}, content: nil, index: nil, **options) ⇒ XmlElementBuilder
constructor
A new instance of XmlElementBuilder.
-
#node ⇒ Nokogiri::XML::Element
The XML element node.
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.
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, **) super(**) @presenter = presenter @name = name @attributes = attributes @content = content @index = index @default_method_args = [] @default_method_args << @index unless @index.nil? end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
3 4 5 |
# File 'app/services/xml_element_builder.rb', line 3 def attributes @attributes end |
#content ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'app/services/xml_element_builder.rb', line 3 def name @name end |
#presenter ⇒ Object (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 |
#build ⇒ Nokogiri::XML::Element
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity
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.}") nil end |
#build_attributes ⇒ Nokogiri::XML::Element
Returns 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) = attr_entry[:object_method] method_args = attr_entry[:args] || [] method_args = @default_method_args + method_args value = presenter.send(, *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.
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) = entry[:object_method] method_args = entry[:args] || [] method_args = @default_method_args + method_args @content = presenter.send(, *method_args) raise ArgumentError, "Content for #{name} cannot be nil" if content.nil? && !allow_empty_content content end |
#element ⇒ Nokogiri::XML::Element
Returns 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 |
#node ⇒ Nokogiri::XML::Element
Returns the XML element node.
75 76 77 |
# File 'app/services/xml_element_builder.rb', line 75 def node @node ||= build end |