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.
- #filter(klass) ⇒ Object
-
#initialize(children = nil) ⇒ Node
constructor
Initialize the node.
-
#text ⇒ Object
Any lines of text associated with this node.
-
#traverse {|_self, descend| ... } ⇒ Object
Traverse the tags from this node using #each.
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
#children ⇒ Object (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.
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 |
#text ⇒ Object
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.
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 |