Class: TallyGem::SplitBy::Recursive

Inherits:
Object
  • Object
show all
Defined in:
lib/tallygem/tally.rb

Class Method Summary collapse

Class Method Details

.split(forest, recursive = true, depth = 1, task = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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