DecodeSourceDecodeCommentNode

class Node

Represents a node in a comment tree structure.

Definitions

def initialize(children)

Initialize the node.

Signature

parameter children Array(Node | Text)?

The initial children array containing both structured nodes and text content.

Implementation

def initialize(children)
	@children = children
end

def children?

Whether this node has any children nodes. Ignores class Decode::Comment::Text instances.

Signature

returns bool

Implementation

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

def add(child)

Add a child node to this node.

Signature

parameter child Node | Text

The node to add.

Implementation

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

attr :children

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

Signature

attribute Array(Node | Text)?

The children of this node.

def each(&block)

Enumerate all non-text children nodes.

Signature

yields {|node| process each node}
parameter node Node

A structured child node (Text nodes are filtered out).

returns Enumerator(Node)

Returns an enumerator if no block given.

returns self

Otherwise returns self.

Implementation

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

def filter(klass)

Filter children nodes by class type.

Signature

parameter klass Class

The class to filter by.

yields {|node| process each filtered node}
parameter node Object

A child node that is an instance of klass.

returns Enumerator(Node)

Returns an enumerator if no block given.

returns self

Otherwise returns self.

Implementation

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

def text

Any lines of text associated with this node.

Signature

returns Array(String)?

The lines of text.

Implementation

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

def traverse(&block)

Traverse the tags from this node using Decode::Comment::Node#each. Invoke descend.call(child) to recursively traverse the specified child.

Signature

yields {|node, descend| descend.call}
parameter node Node

The current node which is being traversed.

parameter descend Proc

The recursive method for traversing children.

Implementation

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

def extract_text

Extract text lines from Text children of this node.

Signature

returns Array(String)?

Array of text lines, or nil if no children.

Implementation

def extract_text
	if children = @children
		children.filter_map do |child|
			child.line if child.is_a?(Text)
		end
	end
end