]> projects.mako.cc - rubyvote/commitdiff
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 b3bdcd6d71a253671b795a59c70f471d73c1ab15..5f103fb7f659f90e7fe0e01f9f049420afc77cbd 100644 (file)
@@ -62,23 +62,33 @@ class CondorcetVote < ElectionVote
     end
   end
 
     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
   protected
   def verify_vote(vote=nil)
     vote.instance_of?( Array ) and
       vote == vote.uniq
   end
-  
+
 end
 
 class PureCondorcetVote < CondorcetVote
 end
 
 class PureCondorcetVote < CondorcetVote
-  def result
-    PureCondorcetResult.new( self )
+  def resultFactory(init)
+    PureCondorcetResult.new(init)
   end
 end
 
 class CloneproofSSDVote < CondorcetVote
   end
 end
 
 class CloneproofSSDVote < CondorcetVote
-  def result
-    CloneproofSSDResult.new( self )
+  def resultFactory(init)
+    CloneproofSSDResult.new(init)
   end
 end
 
   end
 end
 
index 7fd8396c046d9b71fd09c75e2f35866ddbe5cf97..4499233ebb0bda4e0f305c6a853bbbf7c96ed454 100644 (file)
@@ -61,6 +61,16 @@ class ElectionVote
   def tally_vote
     self.verify_vote(vote)
   end
   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
 end
 
 class PluralityVote < ElectionVote
@@ -109,6 +119,7 @@ end
 
 class ElectionResult
   attr_reader :winners
 
 class ElectionResult
   attr_reader :winners
+  attr_accessor :full_results
 
   def initialize(voteobj=nil)
     unless voteobj and voteobj.kind_of?( ElectionVote )
 
   def initialize(voteobj=nil)
     unless voteobj and voteobj.kind_of?( ElectionVote )
@@ -117,6 +128,7 @@ class ElectionResult
 
     @election = voteobj
     @winners = Array.new
 
     @election = voteobj
     @winners = Array.new
+    @full_results = Array.new
   end
 
   def winner
   end
 
   def winner
@@ -127,6 +139,10 @@ class ElectionResult
     @winners.length > 0
   end
 
     @winners.length > 0
   end
 
+  def get_full_results
+    @full_results.collect {|x| x.winners}
+  end
+
 end
 
 class PluralityResult < ElectionResult
 end
 
 class PluralityResult < ElectionResult
index 7ae4a70c2e9e43e2b3a6e5b8b1f0501052c583df..b0df86501ab237c6eca6f0502509bd33b157a1d6 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]
     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
   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] 
     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
   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]
     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
   end
 end

Benjamin Mako Hill || Want to submit a patch?