class TallyGem::SplitBy::Recursive

Public Class Methods

split(forest, recursive = true, depth = 1, task = nil) click to toggle source
# File lib/tallygem/tally.rb, line 11
def self.split(forest, recursive = true, depth = 1, task = nil)
  set = Set.new

  forest.each do |tree|
    # get the current task
    task = tree[:task] if tree.key? :task

    if tree.key?(:subvotes) && !tree[:subvotes].empty?
      tree[:subvotes].each do |sv|
        sv[:task]  ||= task unless task.nil?
        sv[:depth] ||= depth
      end

      set.merge split(tree[:subvotes], recursive, depth + 1, task)

      tree[:subvotes].clear unless recursive
    end

    # add the parent vote
    set.add tree
  end
  set
end