Class: TallyGem::Tally

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quest, partitioner = Partitions::Block) ⇒ Tally

Returns a new instance of Tally.



46
47
48
49
50
51
# File 'lib/tallygem/tally.rb', line 46

def initialize(quest, partitioner = Partitions::Block)
  @quest        = quest
  @result       = nil
  @total_voters = 0
  @partitioner  = partitioner
end

Instance Attribute Details

#questObject (readonly)

Returns the value of attribute quest.



44
45
46
# File 'lib/tallygem/tally.rb', line 44

def quest
  @quest
end

#resultObject (readonly)

Returns the value of attribute result.



44
45
46
# File 'lib/tallygem/tally.rb', line 44

def result
  @result
end

#total_votersObject (readonly)

Returns the value of attribute total_voters.



44
45
46
# File 'lib/tallygem/tally.rb', line 44

def total_voters
  @total_voters
end

Instance Method Details

#runObject

TODO: Plans and nominations



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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