Class: TreeBranch::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_branch/processor.rb

Overview

This class understands how to take a tree, digest it given a context, set of comparators, and a block, then returns a new tree structure.

Instance Method Summary collapse

Instance Method Details

#process(node, context: nil, comparators: [], &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tree_branch/processor.rb', line 14

def process(node, context: nil, comparators: [], &block)
  return nil if at_least_one_comparator_returns_false?(node.data, context, comparators)

  valid_children = process_children(node.children, context, comparators, &block)

  if block_given?
    yield(node.data, valid_children, context)
  else
    ::TreeBranch::Node.new(node.data)
                      .add(valid_children)
  end
end