Added get_full_results method that returns a ranked list of candidates.
authorJoe Slag <joe@slagwerks.com>
Tue, 13 Mar 2007 16:11:03 +0000 (16:11 +0000)
committerJoe Slag <joe@slagwerks.com>
Tue, 13 Mar 2007 16:11:03 +0000 (16:11 +0000)
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

lib/rubyvote/condorcet.rb
lib/rubyvote/election.rb
test/condorcet_test.rb

index b3bdcd6..5f103fb 100644 (file)
@@ -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
 
index 7fd8396..4499233 100644 (file)
@@ -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
index 7ae4a70..b0df865 100644 (file)
@@ -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

Benjamin Mako Hill || Want to submit a patch?