From af3fdc3766e13c5e7b0aa01023285b25303d901f Mon Sep 17 00:00:00 2001 From: Justin Sharps Date: Wed, 15 Aug 2007 22:12:45 +0000 Subject: [PATCH 1/1] Added matrix to condorcet to draw a table of winners vs losers. Also added "points" attr_readers and instance variables to BordaResults and PluralityResults so that they will be availabel directly from the result objects. git-svn-id: svn://rubyforge.org/var/svn/rubyvote/trunk@38 1440c7f4-e209-0410-9a04-881b5eb134a8 --- lib/rubyvote/condorcet.rb | 6 ++++-- lib/rubyvote/election.rb | 5 ++++- lib/rubyvote/positional.rb | 9 ++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/rubyvote/condorcet.rb b/lib/rubyvote/condorcet.rb index aaa5044..bf4e548 100644 --- a/lib/rubyvote/condorcet.rb +++ b/lib/rubyvote/condorcet.rb @@ -32,7 +32,7 @@ ## the CloneproofSSDVote classes but should not be used directly. class CondorcetVote < ElectionVote - + attr_accessor :results def initialize(votes=nil) @@ -144,15 +144,17 @@ end ## directly. class CondorcetResult < ElectionResult + attr_reader :matrix + def initialize(voteobj=nil) unless voteobj and voteobj.kind_of?( CondorcetVote ) raise ArgumentError, "You must pass a CondorcetVote array.", caller end super(voteobj) + @matrix = voteobj.votes end protected - def defeats(candidates=nil, votes=nil) candidates = @election.candidates unless candidates votes = @election.votes unless votes diff --git a/lib/rubyvote/election.rb b/lib/rubyvote/election.rb index 42f18bb..ffd31c8 100644 --- a/lib/rubyvote/election.rb +++ b/lib/rubyvote/election.rb @@ -140,7 +140,8 @@ end class PluralityResult < ElectionResult attr_reader :ranked_candidates - + attr_reader :points + def initialize(voteobj=nil) super(voteobj) @@ -151,6 +152,8 @@ class PluralityResult < ElectionResult b[1] <=> a[1] end.collect {|a| a[0]} + @points = @election.votes + # winners are anyone who has the same number of votes as the # first person @winners = @ranked_candidates.find_all do |i| diff --git a/lib/rubyvote/positional.rb b/lib/rubyvote/positional.rb index 3de3fb2..056194b 100644 --- a/lib/rubyvote/positional.rb +++ b/lib/rubyvote/positional.rb @@ -38,7 +38,7 @@ class BordaVote < ElectionVote end super(votes) end - + def tally_vote(vote) points = candidates.length - 1 vote.each do |candidate| @@ -51,7 +51,7 @@ class BordaVote < ElectionVote points -= 1 end end - + def verify_vote(vote=nil) vote.instance_of?( Array ) and vote == vote.uniq @@ -64,11 +64,12 @@ end class BordaResult < ElectionResult attr_reader :ranked_candidates + attr_reader :points def initialize(voteobj=nil) super(voteobj) votes = @election.votes - + @ranked_candidates = votes.sort do |a, b| b[1] <=> a[1] end.collect {|i| i[0]} @@ -76,6 +77,8 @@ class BordaResult < ElectionResult @winners = @ranked_candidates.find_all do |i| votes[i] == votes[@ranked_candidates[0]] end + + @points = @election.votes end end -- 2.30.2