class Decode::Comment::Node

Attributes

children[R]

Any children of this node. @attribute [Array(Node | Text) | Nil]

Public Class Methods

new(children = nil) click to toggle source

Initialize the node. @parameter children [Array(Node) | Nil]

# File lib/decode/comment/node.rb, line 26
def initialize(children = nil)
        @children = children
end

Public Instance Methods

add(child) click to toggle source

Add a child node to this node. @parameter child [Node] The node to add.

# File lib/decode/comment/node.rb, line 39
def add(child)
        @children ||= []
        @children << child
end
children?() click to toggle source

Whether this node has any children nodes. Ignores {Text} instances. @returns [Boolean]

# File lib/decode/comment/node.rb, line 33
def children?
        @children&.any?{|child| child.is_a?(Node)}
end
each() { |child| ... } click to toggle source

Enumerate all non-text children nodes.

# 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
filter(klass) { |child| ... } click to toggle source
# File lib/decode/comment/node.rb, line 57
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() click to toggle source

Any lines of text associated wtih this node. @returns [Array(String) | Nil] The lines of text.

# File lib/decode/comment/node.rb, line 67
def text
        if text = self.extract_text
                return text if text.any?
        end
end
traverse() { |self, descend| ... } click to toggle source

Traverse the tags from this node using {each}. Invoke `descend.call(child)` to recursively traverse the specified child.

@yields {|node, descend| descend.call}

@parameter node [Node] The current node which is being traversed.
@parameter descend [Proc | Nil] The recursive method for traversing children.
# File lib/decode/comment/node.rb, line 79
def traverse(&block)
        descend = ->(node){
                node.traverse(&block)
        }
        
        yield(self, descend)
end

Protected Instance Methods

extract_text() click to toggle source
# File lib/decode/comment/node.rb, line 89
def extract_text
        if children = @children
                @children.select{|child| child.kind_of?(Text)}.map(&:line)
        else
                nil
        end
end