X-Git-Url: https://projects.mako.cc/source/rubyvote/blobdiff_plain/74e5c5f3c50a36d8d8589c243395b9158977f4db..0ef8f53fb812bcc40337a92f9c4d11ab193f73a9:/lib/rubyvote/range.rb?ds=sidebyside 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