From: Joe Slag Date: Tue, 13 Mar 2007 16:11:03 +0000 (+0000) Subject: Added get_full_results method that returns a ranked list of candidates. X-Git-Url: https://projects.mako.cc/source/rubyvote/commitdiff_plain/f4daa2d9ecfe54c85e0917abddfe57dfe5fc54e6?ds=sidebyside Added get_full_results method that returns a ranked list of candidates. At this time, only Condorcet votes collect the appropriate information. git-svn-id: svn://rubyforge.org/var/svn/rubyvote/trunk@16 1440c7f4-e209-0410-9a04-881b5eb134a8 --- diff --git a/lib/rubyvote/condorcet.rb b/lib/rubyvote/condorcet.rb index b3bdcd6..5f103fb 100644 --- a/lib/rubyvote/condorcet.rb +++ b/lib/rubyvote/condorcet.rb @@ -62,23 +62,33 @@ class CondorcetVote < ElectionVote end end + def result + top_result = resultFactory( self ) + until @candidates.empty? + aResult = resultFactory( self ) + top_result.full_results << aResult + filter_out(aResult) + end + top_result + end + protected def verify_vote(vote=nil) vote.instance_of?( Array ) and vote == vote.uniq end - + end class PureCondorcetVote < CondorcetVote - def result - PureCondorcetResult.new( self ) + def resultFactory(init) + PureCondorcetResult.new(init) end end class CloneproofSSDVote < CondorcetVote - def result - CloneproofSSDResult.new( self ) + def resultFactory(init) + CloneproofSSDResult.new(init) end end diff --git a/lib/rubyvote/election.rb b/lib/rubyvote/election.rb index 7fd8396..4499233 100644 --- a/lib/rubyvote/election.rb +++ b/lib/rubyvote/election.rb @@ -61,6 +61,16 @@ class ElectionVote def tally_vote self.verify_vote(vote) end + + def filter_out(winner) + if winner.winners[0].class == Array + to_filter = winner.winners[0] + else + to_filter = [winner.winners[0]] + end + @candidates.delete_if {|x| to_filter.include?(x)} + end + end class PluralityVote < ElectionVote @@ -109,6 +119,7 @@ end class ElectionResult attr_reader :winners + attr_accessor :full_results def initialize(voteobj=nil) unless voteobj and voteobj.kind_of?( ElectionVote ) @@ -117,6 +128,7 @@ class ElectionResult @election = voteobj @winners = Array.new + @full_results = Array.new end def winner @@ -127,6 +139,10 @@ class ElectionResult @winners.length > 0 end + def get_full_results + @full_results.collect {|x| x.winners} + end + end class PluralityResult < ElectionResult diff --git a/test/condorcet_test.rb b/test/condorcet_test.rb index 7ae4a70..b0df865 100644 --- a/test/condorcet_test.rb +++ b/test/condorcet_test.rb @@ -27,6 +27,8 @@ class TestCondorcetVote < Test::Unit::TestCase 8.times {vote_array << "EBADC".split("")} assert_equal "E", CloneproofSSDVote.new(vote_array).result.winners[0] + assert_equal [['E'], ['A'], ['C'], ['B'], ['D']], + CloneproofSSDVote.new(vote_array).result.get_full_results end def test_ssd2 @@ -42,6 +44,8 @@ class TestCondorcetVote < Test::Unit::TestCase 4.times {vote_array << "DCBA".split("")} assert_equal "D", CloneproofSSDVote.new(vote_array).result.winners[0] + assert_equal [['D'], ['A'], ['C'], ['B']], + CloneproofSSDVote.new(vote_array).result.get_full_results end def test_ssd3 @@ -52,5 +56,7 @@ class TestCondorcetVote < Test::Unit::TestCase 2.times {vote_array << "CBDA".split("")} assert_equal "B", CloneproofSSDVote.new(vote_array).result.winners[0] + assert_equal [['B'], ['C'], ['D'], ['A']], + CloneproofSSDVote.new(vote_array).result.get_full_results end end