Class: Decode::Comment::Node
- Inherits:
-
Object
- Object
- Decode::Comment::Node
- Defined in:
- lib/decode/comment/node.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Any children of this node.
Instance Method Summary collapse
-
#add(child) ⇒ Object
Add a child node to this node.
-
#children? ⇒ Boolean
Whether this node has any children nodes.
-
#each(&block) ⇒ Object
Enumerate all non-text children nodes.
-
#initialize(children = nil) ⇒ Node
constructor
Initialize the node.
-
#text ⇒ Object
Any lines of text associated wtih this node.
-
#traverse {|_self, descend| ... } ⇒ Object
Traverse the tags from this node using #each.
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
#children ⇒ Object (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.
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 |
#text ⇒ Object
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.
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 |