Class: Lutaml::Model::Xml::NokogiriElement

Inherits:
XmlElement
  • Object
show all
Defined in:
lib/lutaml/model/xml/nokogiri_adapter.rb

Constant Summary

Constants inherited from XmlElement

XmlElement::XML_NAMESPACE_URI

Instance Attribute Summary

Attributes inherited from XmlElement

#adapter_node, #attributes, #children, #namespace_prefix, #parent_document

Instance Method Summary collapse

Methods inherited from XmlElement

#[], #add_namespace, #attribute_is_namespace?, #cdata, #cdata_children, #default_namespace, #document, #find_attribute_value, #find_child_by_name, #find_children_by_name, #name, #namespace, #namespaced_name, #namespaces, #nil_element?, #order, #own_namespaces, #pretty_print_instance_variables, #root, #text, #text_children, #to_h, #unprefixed_name

Constructor Details

#initialize(node, root_node: nil, default_namespace: nil) ⇒ NokogiriElement



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/lutaml/model/xml/nokogiri_adapter.rb', line 118

def initialize(node, root_node: nil, default_namespace: nil)
  if root_node
    node.namespaces.each do |prefix, name|
      namespace = XmlNamespace.new(name, prefix)

      root_node.add_namespace(namespace)
    end
  end

  attributes = {}

  # Using `attribute_nodes` instead of `attributes` because
  # `attribute_nodes` handles name collisions as well
  # More info: https://devdocs.io/nokogiri/nokogiri/xml/node#method-i-attributes
  node.attribute_nodes.each do |attr|
    name = if attr.namespace
             "#{attr.namespace.prefix}:#{attr.name}"
           else
             attr.name
           end

    attributes[name] = XmlAttribute.new(
      name,
      attr.value,
      namespace: attr.namespace&.href,
      namespace_prefix: attr.namespace&.prefix,
    )
  end

  if root_node.nil? && !node.namespace&.prefix
    default_namespace = node.namespace&.href
  end

  super(
    node,
    attributes,
    parse_all_children(node, root_node: root_node || self,
                             default_namespace: default_namespace),
    node.text,
    name: node.name,
    parent_document: root_node,
    namespace_prefix: node.namespace&.prefix,
    default_namespace: default_namespace
  )
end

Instance Method Details

#build_xml(builder = nil) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/lutaml/model/xml/nokogiri_adapter.rb', line 179

def build_xml(builder = nil)
  builder ||= Builder::Nokogiri.build

  if name == "text"
    builder.text(text)
  else
    builder.public_send(name, build_attributes(self)) do |xml|
      children.each do |child|
        child.build_xml(xml)
      end
    end
  end

  builder
end

#inner_xmlObject



175
176
177
# File 'lib/lutaml/model/xml/nokogiri_adapter.rb', line 175

def inner_xml
  children.map(&:to_xml).join
end

#text?Boolean



164
165
166
167
# File 'lib/lutaml/model/xml/nokogiri_adapter.rb', line 164

def text?
  # false
  children.empty? && text.length.positive?
end

#to_xmlObject



169
170
171
172
173
# File 'lib/lutaml/model/xml/nokogiri_adapter.rb', line 169

def to_xml
  return text if text?

  build_xml.doc.root.to_xml
end