Test for Pruner
This commit is contained in:
@@ -1,27 +1,29 @@
|
||||
module Pruning
|
||||
module Processing
|
||||
# Contains methods that store the tree and prune everything
|
||||
# except the branches with requested indicator
|
||||
class Pruner
|
||||
|
||||
def initialize(tree)
|
||||
@tree = tree
|
||||
end
|
||||
|
||||
def prune_tree(indicator_ids)
|
||||
pruned_tree = @tree.dup
|
||||
pruned_tree = @tree
|
||||
prune_subtree(pruned_tree, indicator_ids)
|
||||
pruned_tree
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def prune_subtree(nodes, indicator_ids)
|
||||
nodes_to_examine = nodes.dup
|
||||
nodes_to_examine.each do |node|
|
||||
if indicator_node?(node)
|
||||
unwanted_indicator = !indicator_ids.include?(node['id'])
|
||||
nodes.delete(node) if unwanted_indicator
|
||||
else
|
||||
has_no_wanted_indicators_in_children = prune_subtree(children(node), indicator_ids)
|
||||
nodes.delete(node) if has_no_wanted_indicators_in_children
|
||||
if indicator_node?(node)
|
||||
indicator_not_wanted = !indicator_ids.include?(node['id'])
|
||||
nodes.delete(node) if indicator_not_wanted
|
||||
else
|
||||
no_wanted_children = prune_subtree(children(node), indicator_ids)
|
||||
nodes.delete(node) if no_wanted_children
|
||||
end
|
||||
end
|
||||
nodes.empty?
|
||||
@@ -29,11 +31,11 @@ module Pruning
|
||||
|
||||
def children(node)
|
||||
node.fetch('sub_themes', false) ||
|
||||
node.fetch('categories', false) ||
|
||||
node.fetch('indicators', false) ||
|
||||
[]
|
||||
node.fetch('categories', false) ||
|
||||
node.fetch('indicators', false) ||
|
||||
[]
|
||||
end
|
||||
|
||||
|
||||
def indicator_node?(node)
|
||||
children(node).empty?
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user