]> projects.mako.cc - selectricity/blob - lib/rubyvote/range.rb
Merge from head
[selectricity] / lib / rubyvote / range.rb
1 # Range voting as described in wikipedia.
2 # (http://en.wikipedia.org/wiki/Range_voting)
3
4 class RangeVote < ElectionVote
5   def initialize(votes = nil, range = 1..10)
6     @valid_range = range
7     super(votes)
8   end
9
10   def result
11     RangeResult.new(self)
12   end
13
14   protected
15   def verify_vote(vote=nil)
16     vote.instance_of?(Hash) && vote.all?{|c,score| @valid_range.include?(score)}
17   end
18
19   def tally_vote(vote)
20     vote.each do |candidate, score|
21       if @votes.has_key?(candidate)
22         @votes[candidate] += score
23       else
24         @votes[candidate] = score
25         @candidates << candidate
26       end
27     end
28   end
29 end
30
31 class RangeResult < PluralityResult
32 end

Benjamin Mako Hill || Want to submit a patch?