Sha256: 1e48dedc296ebadc923b72c85b2019f88ec813a383558c1b1154499dc3f1b524

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

# -*- coding: utf-8 -*-
module TreeVisitor
  #
  # More complex TreeNodeVisitor
  #
  class TreeNodeVisitor

    def initialize(&block)
      @on_enter_tree_node_blocks = []
      @on_exit_tree_node_blocks = []
      @on_visit_leaf_node_blocks = []
      if block
        instance_eval(&block)
      end
    end

    #
    # called on tree node at start of the visit i.e. we start to visit the subtree
    #
    def enter_tree_node(tree_node)
      @on_enter_tree_node_blocks.each{ |b| b.call(tree_node) }
    end

    #
    # called on tree node at end of the visit i.e. oll subtree are visited
    #
    def exit_tree_node(tree_node)
      @on_exit_tree_node_blocks.each{ |b| b.call(tree_node) }
    end

    #
    # called when visit leaf node
    #
    def visit_leaf_node(leaf_node)
      @on_visit_leaf_node_blocks.each{ |b| b.call(leaf_node) }
    end

    private

    def on_enter_tree_node(&block)
      raise "block missing" unless block
      @on_enter_tree_node_blocks << block
    end

    def on_exit_tree_node(&block)
      raise "block missing" unless block
      @on_exit_tree_node_blocks << block
    end

    def on_visit_leaf_node(&block)
      raise "block missing" unless block
      @on_visit_leaf_node_blocks << block
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
treevisitor-0.1.6 lib/treevisitor/tree_node_visitor.rb