Class: TreeRb::CloneTreeNodeVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_rb/visitors/clone_tree_node_visitor.rb

Overview

Clone a tree_node, nodes are duplicated. Node content are not duplicated!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCloneTreeNodeVisitor

Returns a new instance of CloneTreeNodeVisitor.



14
15
16
17
# File 'lib/tree_rb/visitors/clone_tree_node_visitor.rb', line 14

def initialize
  @cloned_root = nil
  @stack = []
end

Instance Attribute Details

#cloned_rootObject (readonly)

Contains the cloned tree node after the visit



12
13
14
# File 'lib/tree_rb/visitors/clone_tree_node_visitor.rb', line 12

def cloned_root
  @cloned_root
end

Instance Method Details

#cannot_enter_node(tree_node, error) ⇒ Object

called when the tree node is not accessible or an exception is raise when the node is accessed



36
37
# File 'lib/tree_rb/visitors/clone_tree_node_visitor.rb', line 36

def cannot_enter_node( tree_node, error)
end

#enter_node(tree_node) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/tree_rb/visitors/clone_tree_node_visitor.rb', line 19

def enter_node( tree_node )
  if @stack.empty?
    cloned_tree_node = TreeNode.new( tree_node.content )
    @cloned_root = cloned_tree_node
  else
    cloned_tree_node = TreeNode.new( tree_node.content, @stack.last )
  end
  @stack.push( cloned_tree_node )
end

#exit_node(tree_node) ⇒ Object



29
30
31
# File 'lib/tree_rb/visitors/clone_tree_node_visitor.rb', line 29

def exit_node( tree_node )
  @stack.pop
end

#visit_leaf(leaf_node) ⇒ Object



39
40
41
# File 'lib/tree_rb/visitors/clone_tree_node_visitor.rb', line 39

def visit_leaf( leaf_node )
  LeafNode.new( leaf_node.content, @stack.last )
end