class TallyGem::Tally

Attributes

quest[R]
result[R]
total_voters[R]

Public Class Methods

new(quest, partitioner = Partitions::Block) click to toggle source
# File lib/tallygem/tally.rb, line 46
def initialize(quest, partitioner = Partitions::Block)
  @quest        = quest
  @result       = nil
  @total_voters = 0
  @partitioner  = partitioner
end

Public Instance Methods

run() click to toggle source

TODO: Plans and nominations

# File lib/tallygem/tally.rb, line 54
def run
  return @result unless @result.nil?

  # remove any posts that don't have votes
  posts = @quest.posts.reject { |p| p.votes.empty? }

  # filter so that only the latest vote post by the author exists
  author_map = {}
  posts.each do |post|
    if !author_map.key?(post.author) || post.id > author_map[post.author].id
      author_map[post.author] = post
    end
  end
  @total_voters = author_map.size

  posts = author_map.values

  @result = posts.each_with_object({}) do |post, counts|
    @partitioner.split(post.votes).each do |vote|
      nws               = squash_and_clean(vote)
      task              = vote[:task]
      counts[task]      ||= {}
      counts[task][nws] ||= { vote: vote, posts: [] }
      counts[task][nws][:posts] << post
    end
  end
end

Private Instance Methods

squash_and_clean(tree) click to toggle source
# File lib/tallygem/tally.rb, line 84
def squash_and_clean(tree)
  return if tree.empty?
  tree             = tree.clone
  tree[:vote_text] = tree[:vote_text].clone.gsub(/[^\w]/, '').downcase if tree.key?(:vote_text)
  tree[:subvotes]  = tree[:subvotes].collect { |sv| squash_and_clean(sv) } if tree.key?(:subvotes)
  tree
end