Sha256: 86eb9ad4c17463cf164ba09449b8423c24abfe75f9577473ae35c2c6d59289d5

Contents?: true

Size: 974 Bytes

Versions: 3

Compression:

Stored size: 974 Bytes

Contents

# -*- coding: utf-8 -*-
module TreeRb
  #
  # Clone a tree_node, nodes are duplicated.
  # Node content are not duplicated!
  #
  class CloneTreeNodeVisitor # < BasicTreeNodeVisitor

    #
    # Contains the cloned tree node after the visit
    #
    attr_reader :cloned_root

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

    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

    def exit_node( tree_node )
      @stack.pop
    end

    #
    # called when the tree node is not accessible or an exception is raise when the node is accessed
    #
    def cannot_enter_node( tree_node, error)
    end

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

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tree.rb-0.3.13 lib/tree_rb/visitors/clone_tree_node_visitor.rb
tree.rb-0.3.12 lib/tree_rb/visitors/clone_tree_node_visitor.rb
tree.rb-0.3.11 lib/tree_rb/visitors/clone_tree_node_visitor.rb