Class: Shale::Adapter::Nokogiri::Node Private

Inherits:
Object
  • Object
show all
Defined in:
lib/shale/adapter/nokogiri/node.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Wrapper around Nokogiri::XML::Node API

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize object with Nokogiri node

Parameters:

  • node (::Nokogiri::XML::Node)

    Nokogiri node



15
16
17
# File 'lib/shale/adapter/nokogiri/node.rb', line 15

def initialize(node)
  @node = node
end

Instance Method Details

#attributesHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return all attributes associated with the node

Returns:

  • (Hash)


52
53
54
55
56
57
# File 'lib/shale/adapter/nokogiri/node.rb', line 52

def attributes
  @node.attribute_nodes.each_with_object({}) do |node, hash|
    name = [node.namespace&.href, node.name].compact.join(':')
    hash[name] = node.value
  end
end

#childrenArray<Shale::Adapter::Nokogiri::Node>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return node’s element children

Returns:



75
76
77
78
79
80
81
# File 'lib/shale/adapter/nokogiri/node.rb', line 75

def children
  @node
    .children
    .to_a
    .filter(&:element?)
    .map { |e| self.class.new(e) }
end

#nameString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

without namespace

node.name # => Bar

with namespace

node.name # => http://foo:Bar

Returns:

  • (String)


43
44
45
# File 'lib/shale/adapter/nokogiri/node.rb', line 43

def name
  [@node.namespace&.href, @node.name].compact.join(':')
end

#namespacesHash<String, String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return namespaces defined on document

Examples:

node.namespaces # => { 'foo' => 'http://foo.com', 'bar' => 'http://bar.com' }

Returns:

  • (Hash<String, String>)


27
28
29
# File 'lib/shale/adapter/nokogiri/node.rb', line 27

def namespaces
  @node.namespaces.transform_keys { |e| e.sub('xmlns:', '') }
end

#parentShale::Adapter::Nokogiri::Node?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return node’s parent



64
65
66
67
68
# File 'lib/shale/adapter/nokogiri/node.rb', line 64

def parent
  if @node.respond_to?(:parent) && @node.parent && @node.parent.name != 'document'
    self.class.new(@node.parent)
  end
end

#textString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return first text child of a node

Returns:

  • (String)


88
89
90
91
92
93
94
95
96
# File 'lib/shale/adapter/nokogiri/node.rb', line 88

def text
  first = @node
    .children
    .to_a
    .filter { |e| e.text? || e.cdata? }
    .first

  first&.text
end