Module: Tree::Node::InstanceMethods

Defined in:
lib/tree_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject



29
30
31
# File 'lib/tree_node.rb', line 29

def children
  @children ||= []
end

#parentObject

Returns the value of attribute parent.



11
12
13
# File 'lib/tree_node.rb', line 11

def parent
  @parent
end

Instance Method Details

#add_child(object) ⇒ Object



33
34
35
36
# File 'lib/tree_node.rb', line 33

def add_child object
  object.parent = self
  children << object
end

#map(direction, collecting_object = [], &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/tree_node.rb', line 14

def map(direction, collecting_object = [], &block)
  if direction == :down
    collecting_object << block.call(self)
    children.each { |child| child.map(direction, collecting_object, &block) }
  else
    parent.map(direction, collecting_object, &block) if parent
    collecting_object << block.call(self)
  end
  collecting_object
end

#root(&block) ⇒ Object



25
26
27
# File 'lib/tree_node.rb', line 25

def root(&block)
  parent.root(&block) rescue block.call(self)
end