class Node

A Node is a Hash with an array of children and a parent associated to it.

Attributes

children[R]
parent[RW]

Public Class Methods

new(*args) click to toggle source

Creates a new Node

Calls superclass method
# File lib/glyph/node.rb, line 22
def initialize(*args)
        super(*args)
        @children = []
end

Public Instance Methods

&(index) click to toggle source

See Node#child.

# File lib/glyph/node.rb, line 78
def &(index)
       @children[index]
end
<<(hash) click to toggle source

Adds a child node to self @param [Hash] hash the new child @return [Array] the node's children @raise [ArgumentError] unless a Hash is passed as parameter

# File lib/glyph/node.rb, line 54
def <<(hash)
        raise ArgumentError, "#{hash} is not a Hash" unless hash.is_a? Hash
        ht = hash.to_node
        ht.parent = self
        @children << ht
end
==(node) click to toggle source

@return (Boolean) true if the nodes are equal @since 0.3.0

# File lib/glyph/node.rb, line 159
def ==(node)
        return false unless node.is_a? Node
       self.to_hash == node.to_hash && self.children == node.children
end
>>(node) click to toggle source

Removes a child node from self @param [node] node the child node to remove @raise [ArgumentError] unless an existing child node is passed as parameter

# File lib/glyph/node.rb, line 64
def >>(node)
        raise ArgumentError, "Unknown child node" unless @children.include? node
        node.parent = nil
        @children.delete node
end
ascend(element=nil) { |element| ... } click to toggle source

Iterates through parents recursively (including self) @param [Node, nil] element the node to process @yieldparam [Node] element the current node

# File lib/glyph/node.rb, line 129
def ascend(element=nil, &block)
        element ||= self
        yield element
        ascend(element.parent, &block) if element.parent
end
child(index) click to toggle source

Returns a child by its index @return [Node] the child node or nil @param [Integer] index the child index

# File lib/glyph/node.rb, line 73
def child(index)
        @children[index]
end
clear() click to toggle source

Clears all keys, parent and children

Calls superclass method
# File lib/glyph/node.rb, line 44
def clear
        super
        @children.clear
        @parent = nil
end
descend(element=nil, level=0) { |element, level| ... } click to toggle source

Iterates through children recursively (including self) @param [Node, nil] element the node to process @yieldparam [Node] element the current node @yieldparam [Integer] level the current tree depth

# File lib/glyph/node.rb, line 93
def descend(element=nil, level=0, &block)
        element ||= self
        yield element, level
        element.each_child do |c| 
                descend c, level+1, &block 
        end
end
each_child() { |c| ... } click to toggle source

Iterates through children @yieldparam [Node] c the child node

# File lib/glyph/node.rb, line 84
def each_child
        @children.each {|c| yield c }
end
find_child(&block) click to toggle source

Descend children until the block returns something.

Each child is passed to the block.

@param [Proc] &block the block to call on each child @return [Node, nil] returns the child node if found, nil otherwise

# File lib/glyph/node.rb, line 105
def find_child(&block)
        children.each do |c|
                c.descend do |node, level|
                        return node if block.call(node)
                end
        end
        nil
end
find_parent(&block) click to toggle source

Ascend parents until the block returns something.

Each parent is passed to the block.

@param [Proc] &block the block to call on each parent @return [Node, nil] returns the parent node if found, nil otherwise

# File lib/glyph/node.rb, line 118
def find_parent(&block)
        return nil unless parent
        parent.ascend do |node|
                return node if block.call(node)
        end
        nil
end
from(node) click to toggle source

Clone another node (replaces all keys, parent and children) @param [#to_node] node the Node to clone @return [self]

# File lib/glyph/node.rb, line 35
def from(node)
        n = node.to_node
        replace node
        @parent = n.parent
        @children = n.children
        self
end
inspect() click to toggle source

@return [String] a textual representation of self @since 0.3.0

# File lib/glyph/node.rb, line 149
def inspect
        string = ""
        descend do |e, level|
                string << "  "*level+e.to_hash.inspect+"\n"
        end
        string.chomp
end
root() click to toggle source

@return [Node] Returns the root node

# File lib/glyph/node.rb, line 136
def root
        ascend(parent) {|e| return e unless e.parent }
end
to_hash() click to toggle source

Converts self to a hash @return [Hash] the converted hash @since 0.3.0

# File lib/glyph/node.rb, line 143
def to_hash
        {}.merge(self)
end
to_node() click to toggle source

@return [Node] Returns self

# File lib/glyph/node.rb, line 28
def to_node
        self
end