33 lines
786 B
Ruby
33 lines
786 B
Ruby
module Pruning
|
|
module Processing
|
|
class Pruner
|
|
|
|
def initialize(tree, indicator_ids)
|
|
@tree = tree
|
|
end
|
|
|
|
def prune_tree(nodes, indicator_ids)
|
|
nodes.delete_if do |node|
|
|
unwanted_indicator = indicator_node?(node) && !indicator_ids.include?(node['id'])
|
|
has_no_wanted_indicators_in_children = prune_tree(children(node), indicator_ids)
|
|
|
|
unwanted_indicator && has_no_wanted_indicators_in_children
|
|
end
|
|
nodes.empty?
|
|
end
|
|
|
|
private
|
|
def children(node)
|
|
node.get('sub-themes', false) ||
|
|
node.get('categories', false) ||
|
|
node.get('indicators', false) ||
|
|
[]
|
|
end
|
|
|
|
def indicator_node?(node)
|
|
children(node).empty?
|
|
end
|
|
end
|
|
end
|
|
end
|