Class: Decode::Comment::Node

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

Overview

Represents a node in a comment tree structure.

Direct Known Subclasses

Tag, Documentation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(children) ⇒ Node

Initialize the node.



12
13
14
# File 'lib/decode/comment/node.rb', line 12

def initialize(children)
	@children = children
end

Instance Attribute Details

#childrenObject (readonly)

Contains a mix of Node objects (structured comment tags like ‘@parameter`, `@returns`) and Text objects (plain comment text and tag descriptions).



37
38
39
# File 'lib/decode/comment/node.rb', line 37

def children
  @children
end

#The children of this node.(childrenofthisnode.) ⇒ Object (readonly)

Contains a mix of Node objects (structured comment tags like ‘@parameter`, `@returns`) and Text objects (plain comment text and tag descriptions).



37
# File 'lib/decode/comment/node.rb', line 37

attr :children

Instance Method Details

#add(child) ⇒ Object

Add a child node to this node.



25
26
27
28
29
30
31
32
33
# File 'lib/decode/comment/node.rb', line 25

def add(child)
	if children = @children
		children << child
	else
		@children = [child]
	end
	
	return self
end

#children?Boolean

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

Returns:

  • (Boolean)


19
20
21
# File 'lib/decode/comment/node.rb', line 19

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

#each(&block) ⇒ Object

Enumerate all non-text children nodes.



44
45
46
47
48
49
50
51
52
# File 'lib/decode/comment/node.rb', line 44

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

#filter(klass) ⇒ Object

Filter children nodes by class type.



60
61
62
63
64
65
66
67
68
# File 'lib/decode/comment/node.rb', line 60

def filter(klass)
	return to_enum(:filter, klass) unless block_given?
	
	@children&.each do |child|
		yield child if child.is_a?(klass)
	end
	
	return self
end

#textObject

Any lines of text associated with this node.



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

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:



83
84
85
86
87
# File 'lib/decode/comment/node.rb', line 83

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