Class: Lutaml::Model::XmlAdapter::NokogiriElement

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

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, #extract_name, #extract_namespace_prefix, #find_attribute_value, #find_child_by_name, #find_children_by_name, #name, #name_from_node, #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

Returns a new instance of NokogiriElement.



117
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
# File 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb', line 117

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,
    parent_document: root_node,
    namespace_prefix: node.namespace&.prefix,
    default_namespace: default_namespace
  )
end

Instance Method Details

#build_xml(builder = nil) ⇒ Object



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

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



173
174
175
# File 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb', line 173

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

#text?Boolean

Returns:

  • (Boolean)


162
163
164
165
# File 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb', line 162

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

#to_xmlObject



167
168
169
170
171
# File 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb', line 167

def to_xml
  return text if text?

  build_xml.doc.root.to_xml
end