Class: Shale::Adapter::REXML::Node Private

Inherits:
Object
  • Object
show all
Defined in:
lib/shale/adapter/rexml/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 REXML::Element 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 REXML element

Parameters:

  • node (::REXML::Element)

    REXML element



17
18
19
# File 'lib/shale/adapter/rexml/node.rb', line 17

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)


54
55
56
57
58
59
60
61
62
63
# File 'lib/shale/adapter/rexml/node.rb', line 54

def attributes
  attributes = @node.attributes.values.map do |attribute|
    attribute.is_a?(Hash) ? attribute.values : attribute
  end.flatten

  attributes.each_with_object({}) do |attribute, hash|
    name = [Utils.presence(attribute.namespace), attribute.name].compact.join(':')
    hash[name] = attribute.value
  end
end

#childrenArray<Shale::Adapter::REXML::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:



81
82
83
84
85
86
# File 'lib/shale/adapter/rexml/node.rb', line 81

def children
  @node
    .children
    .filter { |e| e.node_type == :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)


45
46
47
# File 'lib/shale/adapter/rexml/node.rb', line 45

def name
  [Utils.presence(@node.namespace), @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>)


29
30
31
# File 'lib/shale/adapter/rexml/node.rb', line 29

def namespaces
  @node.namespaces
end

#parentShale::Adapter::REXML::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

Returns:



70
71
72
73
74
# File 'lib/shale/adapter/rexml/node.rb', line 70

def parent
  if @node.parent && @node.parent.name != ''
    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)


93
94
95
# File 'lib/shale/adapter/rexml/node.rb', line 93

def text
  @node.text
end