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

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

Instance Attribute Summary

Attributes inherited from Element

#attributes, #children, #namespace_prefix, #parent_document, #text

Instance Method Summary collapse

Methods inherited from Element

#add_namespace, #attribute_is_namespace?, #default_namespace, #document, #extract_name, #extract_namespace_prefix, #name, #namespace, #namespaces, #own_namespaces, #unprefixed_name

Constructor Details

#initialize(node, root_node: nil) ⇒ NokogiriElement

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb', line 151

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

      root_node.add_namespace(namespace)
    end
  end

  attributes = {}
  node.attributes.transform_values do |attr|
    name = if attr.namespace
             "#{attr.namespace.prefix}:#{attr.name}"
           else
             attr.name
           end

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

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

Instance Method Details

#text?Boolean

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

Returns:

  • (Boolean)


190
191
192
193
# File 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb', line 190

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

#to_xml(builder = nil) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb', line 195

def to_xml(builder = nil)
  builder ||= Nokogiri::XML::Builder.new

  if name == "text"
    builder.text(text)
  else
    builder.send(name, build_attributes(self)) do |xml|
      children.each { |child| child.to_xml(xml) }
    end
  end

  builder
end