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.



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

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

Instance Attribute Details

#childrenObject (readonly)

Any children of this node.



31
32
33
# File 'lib/decode/comment/node.rb', line 31

def children
  @children
end

Instance Method Details

#add(child) ⇒ Object

Add a child node to this node.



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

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

#children?Boolean

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

Returns:

  • (Boolean)


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

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

#each(&block) ⇒ Object

Enumerate all non-text children nodes.



34
35
36
37
38
39
40
# File 'lib/decode/comment/node.rb', line 34

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

#filter(klass) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/decode/comment/node.rb', line 42

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

#textObject

Any lines of text associated with this node.



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

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:



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

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