Class: Decode::Comment::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/decode/comment/node.rb

Direct Known Subclasses

Tag, Documentation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(children = nil) ⇒ Node

Initialize the node.



26
27
28
# File 'lib/decode/comment/node.rb', line 26

def initialize(children = nil)
  @children = children
end

Instance Attribute Details

#childrenObject (readonly)

Any children of this node.



46
47
48
# File 'lib/decode/comment/node.rb', line 46

def children
  @children
end

Instance Method Details

#add(child) ⇒ Object

Add a child node to this node.



39
40
41
42
# File 'lib/decode/comment/node.rb', line 39

def add(child)
  @children ||= []
  @children << child
end

#children?Boolean

Whether this node has any children nodes. Ignores Text instances.

Returns:

  • (Boolean)


33
34
35
# File 'lib/decode/comment/node.rb', line 33

def children?
  @children&.any?{|child| child.is_a?(Node)}
end

#each(&block) ⇒ Object

Enumerate all non-text children nodes.



49
50
51
52
53
54
55
# File 'lib/decode/comment/node.rb', line 49

def each(&block)
  return to_enum unless block_given?
  
  @children&.each do |child|
    yield child if child.is_a?(Node)
  end
end

#textObject

Any lines of text associated wtih this node.



59
60
61
62
63
# File 'lib/decode/comment/node.rb', line 59

def text
  if text = self.extract_text
    return text if text.any?
  end
end

#traverse {|_self, descend| ... } ⇒ Object

Traverse the tags from this node using #each. Invoke ‘descend.call(child)` to recursively traverse the specified child.

Yields:

  • (_self, descend)

Yield Parameters:



71
72
73
74
75
76
77
# File 'lib/decode/comment/node.rb', line 71

def traverse(&block)
  descend = ->(node){
    node.traverse(&block)
  }
  
  yield(self, descend)
end