123456789_123456789_123456789_123456789_123456789_

Class: ActionText::BottomUpReducer

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: actiontext/lib/action_text/bottom_up_reducer.rb

Class Method Summary

Instance Method Summary

Constructor Details

.new(node) ⇒ BottomUpReducer

[ GitHub ]

  
# File 'actiontext/lib/action_text/bottom_up_reducer.rb', line 5

def initialize(node)
  @node = node
  @values = {}
end

Instance Method Details

#reduce(&block)

[ GitHub ]

  
# File 'actiontext/lib/action_text/bottom_up_reducer.rb', line 10

def reduce(&block)
  traverse_bottom_up(@node) do |n, children|
    @values[n] = block.call(n, children.map { |child| @values[child] })
  end
  @values[@node]
end

#traverse_bottom_up(node) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/bottom_up_reducer.rb', line 18

def traverse_bottom_up(node)
  call_stack, processing_stack, children_stack = [ node ], [], []

  until call_stack.empty?
    node = call_stack.pop
    children = node.children.to_a
    processing_stack.push(node)
    children_stack.push(children)
    call_stack.concat children
  end

  (processing_stack.size - 1).downto(0) do |i|
    yield processing_stack[i], children_stack[i]
  end
end