X-Git-Url: https://projects.mako.cc/source/selectricity/blobdiff_plain/1260415e640fec24f8c970ae8e61f4a7ab4594d4..560828ebdd364de4ca09d61764f19215a9e29e59:/lib/rubyvote/range.rb diff --git a/lib/rubyvote/range.rb b/lib/rubyvote/range.rb new file mode 100644 index 0000000..5c01f68 --- /dev/null +++ b/lib/rubyvote/range.rb @@ -0,0 +1,32 @@ +# Range voting as described in wikipedia. +# (http://en.wikipedia.org/wiki/Range_voting) + +class RangeVote < ElectionVote + def initialize(votes = nil, range = 1..10) + @valid_range = range + super(votes) + end + + def result + RangeResult.new(self) + end + + protected + def verify_vote(vote=nil) + vote.instance_of?(Hash) && vote.all?{|c,score| @valid_range.include?(score)} + end + + def tally_vote(vote) + vote.each do |candidate, score| + if @votes.has_key?(candidate) + @votes[candidate] += score + else + @votes[candidate] = score + @candidates << candidate + end + end + end +end + +class RangeResult < PluralityResult +end