Class: TreeDelta::Traversal

Inherits:
Enumerator
  • Object
show all
Defined in:
lib/tree_delta/traversal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(direction:, order:) ⇒ Traversal

Returns a new instance of Traversal.



5
6
7
8
# File 'lib/tree_delta/traversal.rb', line 5

def initialize(direction:, order:)
  @direction = direction
  @order     = order
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



3
4
5
# File 'lib/tree_delta/traversal.rb', line 3

def direction
  @direction
end

#orderObject (readonly)

Returns the value of attribute order.



3
4
5
# File 'lib/tree_delta/traversal.rb', line 3

def order
  @order
end

Instance Method Details

#traverse(node) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tree_delta/traversal.rb', line 10

def traverse(node)
  Enumerator.new do |y|
    if node
      y.yield(node) if order == :pre

      ordered_children(node).each do |child|
        traverse(child).each { |n| y.yield(n) }
      end

      y.yield(node) if order == :post
    end
  end
end