Class: Shale::Adapter::Nokogiri::Node Private
- Inherits:
-
Object
- Object
- Shale::Adapter::Nokogiri::Node
- 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
-
#attributes ⇒ Hash
private
Return all attributes associated with the node.
-
#children ⇒ Array<Shale::Adapter::Nokogiri::Node>
private
Return node’s element children.
-
#initialize(node) ⇒ Node
constructor
private
Initialize object with Nokogiri node.
-
#name ⇒ String
private
Return name of the node in the format of namespace:name when the node is namespaced or just name when it’s not.
-
#namespaces ⇒ Hash<String, String>
private
Return namespaces defined on document.
-
#parent ⇒ Shale::Adapter::Nokogiri::Node?
private
Return node’s parent.
-
#text ⇒ String
private
Return first text child of a node.
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
15 16 17 |
# File 'lib/shale/adapter/nokogiri/node.rb', line 15 def initialize(node) @node = node end |
Instance Method Details
#attributes ⇒ Hash
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
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 |
#children ⇒ Array<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
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 |
#name ⇒ 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 name of the node in the format of namespace:name when the node is namespaced or just name when it’s not
43 44 45 |
# File 'lib/shale/adapter/nokogiri/node.rb', line 43 def name [@node.namespace&.href, @node.name].compact.join(':') end |
#namespaces ⇒ Hash<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
27 28 29 |
# File 'lib/shale/adapter/nokogiri/node.rb', line 27 def namespaces @node.namespaces.transform_keys { |e| e.sub('xmlns:', '') } end |
#parent ⇒ 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 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 |
#text ⇒ 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 first text child of a node
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 |