Adding the rubyvote library to do the actually election-making. The only
[selectricity-live] / lib / rubyvote / range.rb
diff --git a/lib/rubyvote/range.rb b/lib/rubyvote/range.rb
new file mode 100644 (file)
index 0000000..5c01f68
--- /dev/null
@@ -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

Benjamin Mako Hill || Want to submit a patch?