Class: SXRB::Node

Inherits:
Struct
  • Object
show all
Defined in:
lib/sxrb/node.rb

Overview

Node class is simple DOM-like element, which allows easy travesing through restricted part of document structure with #children and #parent methods.

Direct Known Subclasses

TextNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ 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.

Internal method used to build DOM-like structure.



8
9
10
11
# File 'lib/sxrb/node.rb', line 8

def initialize(*args, &block)
  super(*args, &block)
  @children = []
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes

Returns:

  • (Object)

    the current value of attributes



4
5
6
# File 'lib/sxrb/node.rb', line 4

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



5
6
7
# File 'lib/sxrb/node.rb', line 5

def children
  @children
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



4
5
6
# File 'lib/sxrb/node.rb', line 4

def name
  @name
end

#namespacesObject

Returns the value of attribute namespaces

Returns:

  • (Object)

    the current value of namespaces



4
5
6
# File 'lib/sxrb/node.rb', line 4

def namespaces
  @namespaces
end

#parentObject

Returns the value of attribute parent

Returns:

  • (Object)

    the current value of parent



4
5
6
# File 'lib/sxrb/node.rb', line 4

def parent
  @parent
end

#prefixObject

Returns the value of attribute prefix

Returns:

  • (Object)

    the current value of prefix



4
5
6
# File 'lib/sxrb/node.rb', line 4

def prefix
  @prefix
end

#uriObject

Returns the value of attribute uri

Returns:

  • (Object)

    the current value of uri



4
5
6
# File 'lib/sxrb/node.rb', line 4

def uri
  @uri
end

Instance Method Details

#append(node) ⇒ Object

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.

Internal method used to build DOM-like structure.



15
16
17
18
# File 'lib/sxrb/node.rb', line 15

def append(node)
  node.parent = self
  @children << node
end

#contentString

Returns text content of a node (recursively), skipping all markup.

Returns:

  • (String)

    Text content of this node and all it’s descendants is returned, concatenated.



25
26
27
# File 'lib/sxrb/node.rb', line 25

def content
  children.map {|child| child.is_a?(TextNode)? child.text : child.content}.flatten.join('')
end