Initial commit
This commit is contained in:
32
lib/pruner.rb
Normal file
32
lib/pruner.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
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
|
||||
Reference in New Issue
Block a user