Class: ScoutApm::LayerConverters::DepthFirstWalker

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/layer_converters/depth_first_walker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_layer) ⇒ DepthFirstWalker

Returns a new instance of DepthFirstWalker.



6
7
8
9
10
11
12
# File 'lib/scout_apm/layer_converters/depth_first_walker.rb', line 6

def initialize(root_layer)
  @root_layer = root_layer

  @on_blocks = []
  @before_blocks = []
  @after_blocks = []
end

Instance Attribute Details

#root_layerObject (readonly)

Returns the value of attribute root_layer.



4
5
6
# File 'lib/scout_apm/layer_converters/depth_first_walker.rb', line 4

def root_layer
  @root_layer
end

Instance Method Details

#after(&block) ⇒ Object



18
19
20
# File 'lib/scout_apm/layer_converters/depth_first_walker.rb', line 18

def after(&block)
  @after_blocks << block
end

#before(&block) ⇒ Object



14
15
16
# File 'lib/scout_apm/layer_converters/depth_first_walker.rb', line 14

def before(&block)
  @before_blocks << block
end

#on(&block) ⇒ Object



22
23
24
# File 'lib/scout_apm/layer_converters/depth_first_walker.rb', line 22

def on(&block)
  @on_blocks << block
end

#walk(layer = root_layer) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/scout_apm/layer_converters/depth_first_walker.rb', line 26

def walk(layer=root_layer)
  # Need to run this for the root layer the first time through.
  if layer == root_layer
    @before_blocks.each{|b| b.call(layer) }
    @on_blocks.each{|b| b.call(layer) }
  end

  layer.children.each do |child|
    @before_blocks.each{|b| b.call(child) }
    @on_blocks.each{|b| b.call(child) }
    walk(child)
    @after_blocks.each{|b| b.call(child) }
  end

  if layer == root_layer
    @after_blocks.each{|b| b.call(layer) }
  end

  nil
end