Class: Node

Inherits:
Hash show all
Defined in:
lib/glyph/node.rb

Overview

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

Direct Known Subclasses

Glyph::SyntaxNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#to_options

Constructor Details

#initialize(*args) ⇒ Node

Creates a new Node


22
23
24
25
# File 'lib/glyph/node.rb', line 22

def initialize(*args)
	super(*args)
	@children = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children


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

def children
  @children
end

#parentObject

Returns the value of attribute parent


19
20
21
# File 'lib/glyph/node.rb', line 19

def parent
  @parent
end

Instance Method Details

#&(index) ⇒ Object

See Node#child.


78
79
80
# File 'lib/glyph/node.rb', line 78

def &(index)
 	@children[index]
end

#<<(hash) ⇒ Array

Adds a child node to self

Parameters:

  • hash (Hash)

    the new child

Returns:

  • (Array)

    the node's children

Raises:

  • (ArgumentError)

    unless a Hash is passed as parameter


54
55
56
57
58
59
# 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) ⇒ Boolean

Returns true if the nodes are equal.

Returns:

  • (Boolean)

    true if the nodes are equal

Since:

  • 0.3.0


159
160
161
162
# 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) ⇒ Object

Removes a child node from self

Parameters:

  • node (node)

    the child node to remove

Raises:

  • (ArgumentError)

    unless an existing child node is passed as parameter


64
65
66
67
68
# 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| ... } ⇒ Object

Iterates through parents recursively (including self)

Parameters:

  • element (Node, nil) (defaults to: nil)

    the node to process

Yield Parameters:

  • element (Node)

    the current node


129
130
131
132
133
# 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) ⇒ Node

Returns a child by its index

Parameters:

  • index (Integer)

    the child index

Returns:

  • (Node)

    the child node or nil


73
74
75
# File 'lib/glyph/node.rb', line 73

def child(index)
	@children[index]
end

#clearObject

Clears all keys, parent and children


44
45
46
47
48
# File 'lib/glyph/node.rb', line 44

def clear
	super
	@children.clear
	@parent = nil
end

#descend(element = nil, level = 0) {|element, level| ... } ⇒ Object

Iterates through children recursively (including self)

Parameters:

  • element (Node, nil) (defaults to: nil)

    the node to process

Yield Parameters:

  • element (Node)

    the current node

  • level (Integer)

    the current tree depth


93
94
95
96
97
98
99
# 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| ... } ⇒ Object

Iterates through children

Yield Parameters:

  • c (Node)

    the child node


84
85
86
# File 'lib/glyph/node.rb', line 84

def each_child
	@children.each {|c| yield c }
end

#find_child(&block) ⇒ Node?

Descend children until the block returns something. Each child is passed to the block.

Parameters:

  • &block (Proc)

    the block to call on each child

Returns:

  • (Node, nil)

    returns the child node if found, nil otherwise


105
106
107
108
109
110
111
112
# 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) ⇒ Node?

Ascend parents until the block returns something. Each parent is passed to the block.

Parameters:

  • &block (Proc)

    the block to call on each parent

Returns:

  • (Node, nil)

    returns the parent node if found, nil otherwise


118
119
120
121
122
123
124
# 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) ⇒ self

Clone another node (replaces all keys, parent and children)

Parameters:

  • node (#to_node)

    the Node to clone

Returns:

  • (self)

35
36
37
38
39
40
41
# File 'lib/glyph/node.rb', line 35

def from(node)
	n = node.to_node
	replace node
	@parent = n.parent
	@children = n.children
	self
end

#inspectString

Returns a textual representation of self.

Returns:

  • (String)

    a textual representation of self

Since:

  • 0.3.0


149
150
151
152
153
154
155
# 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

#rootNode

Returns the root node

Returns:

  • (Node)

    Returns the root node


136
137
138
# File 'lib/glyph/node.rb', line 136

def root
	ascend(parent) {|e| return e unless e.parent }
end

#to_hashHash

Converts self to a hash

Returns:

  • (Hash)

    the converted hash

Since:

  • 0.3.0


143
144
145
# File 'lib/glyph/node.rb', line 143

def to_hash
	{}.merge(self)
end

#to_nodeNode

Returns self

Returns:

  • (Node)

    Returns self


28
29
30
# File 'lib/glyph/node.rb', line 28

def to_node
	self
end