class Shale::Adapter::Nokogiri::Node

Wrapper around Nokogiri::XML::Node API

@api private

Public Class Methods

new(node) click to toggle source

Initialize object with Nokogiri node

@param [::Nokogiri::XML::Node] node Nokogiri node

@api private

# File lib/shale/adapter/nokogiri.rb, line 114
def initialize(node)
  @node = node
end

Public Instance Methods

attributes() click to toggle source

Return all attributes associated with the node

@return [Hash]

@api private

# File lib/shale/adapter/nokogiri.rb, line 139
def attributes
  @node.attribute_nodes.each_with_object({}) do |node, hash|
    name = [node.namespace&.prefix, node.name].compact.join(':')
    hash[name] = node.value
  end
end
children() click to toggle source

Return node’s element children

@return [Array<Shale::Adapter::Nokogiri::Node>]

@api private

# File lib/shale/adapter/nokogiri.rb, line 151
def children
  @node
    .children
    .to_a
    .filter(&:element?)
    .map { |e| self.class.new(e) }
end
name() click to toggle source

Return fully qualified name of the node in the format of namespace:name when the node is namespaced or just name when it’s not

@return [String]

@example without namespace

node.name # => Bar

@example with namespace

node.name # => foo:Bar

@api private

# File lib/shale/adapter/nokogiri.rb, line 130
def name
  [@node.namespace&.prefix, @node.name].compact.join(':')
end
text() click to toggle source

Return first text child of a node

@return [String]

@api private

# File lib/shale/adapter/nokogiri.rb, line 164
def text
  first = @node
    .children
    .to_a
    .filter(&:text?)
    .first

  first&.text
end