-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Franklin Webber
committed
Oct 27, 2011
1 parent
3697797
commit 6516eff
Showing
5 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,73 @@ | |||
# encoding: UTF-8 | |||
|
|||
class Robut::Plugin::Quiz::Range | |||
include Robut::Plugin::Quiz::Question | |||
|
|||
RANGE_VALUES = /(\d+)(?:-|\.\.)(\d+)/ | |||
|
|||
def handle_response(sender_nick,response) | |||
|
|||
if within_range? response | |||
store_range_response_for sender_nick, response | |||
true | |||
else | |||
false | |||
end | |||
|
|||
end | |||
|
|||
def start_value | |||
if Array(@parameters).first.to_s =~ RANGE_VALUES | |||
Regexp.last_match(1).to_i | |||
else | |||
1 | |||
end | |||
end | |||
|
|||
def highest_value | |||
if Array(@parameters).first.to_s =~ RANGE_VALUES | |||
Regexp.last_match(2).to_i | |||
else | |||
5 | |||
end | |||
end | |||
|
|||
def within_range?(value) | |||
value.to_i >= start_value and value.to_i <= highest_value | |||
end | |||
|
|||
def ask | |||
"@all Question '#{@question}' (#{start_value}..#{highest_value})" | |||
end | |||
|
|||
def results | |||
return "I'm sorry, not enough votes were cast to be create a summary." if captured_results.length == 0 | |||
|
|||
result_values = captured_results.values.extend Robut::Plugin::Quiz::Range::Averages | |||
"#{result_values.length} votes with a mean of #{result_values.mean}" | |||
end | |||
|
|||
def store_range_response_for(sender_nick,value) | |||
captured_results[sender_nick] = value.to_i | |||
end | |||
|
|||
|
|||
module Averages | |||
|
|||
def sum | |||
inject(:+) | |||
end | |||
|
|||
def mean | |||
return 0 if length == 0 | |||
sum.to_f / length.to_f | |||
end | |||
|
|||
def mode | |||
frequency = inject(Hash.new(0)) { |hash,value| hash[value] += 1; hash } | |||
sort_by {|value| frequency[value] }.last | |||
end | |||
|
|||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,100 @@ | |||
require_relative 'spec_helper' | |||
|
|||
describe Robut::Plugin::Quiz::Range do | |||
|
|||
subject do | |||
Robut::Plugin::Quiz::Range.new 'person',"ask range for 3 minutes 'Should I continue the presentation?'" | |||
end | |||
|
|||
describe "#start_value" do | |||
|
|||
context "when no range has been specified" do | |||
|
|||
it "should default to the value 1" do | |||
subject.start_value.should == 1 | |||
end | |||
|
|||
end | |||
|
|||
end | |||
|
|||
describe "#highest_value" do | |||
|
|||
context "when no range has been specified" do | |||
|
|||
it "should default to the value of 5" do | |||
subject.highest_value.should == 5 | |||
end | |||
|
|||
end | |||
|
|||
end | |||
|
|||
describe "#within_range?" do | |||
|
|||
context "when the value is within range" do | |||
|
|||
it "should respond with true" do | |||
subject.within_range?(3).should be_true | |||
end | |||
|
|||
end | |||
|
|||
context "when the value is not within range" do | |||
|
|||
it "should respond with false" do | |||
subject.within_range?(6).should be_false | |||
end | |||
|
|||
end | |||
|
|||
end | |||
|
|||
describe "#handle_response" do | |||
|
|||
context "with a valid response" do | |||
|
|||
it "should return true" do | |||
subject.should_receive(:store_range_response_for).with("sender","1") | |||
subject.handle_response("sender","1").should be_true | |||
end | |||
|
|||
end | |||
|
|||
end | |||
|
|||
describe Robut::Plugin::Quiz::Range::Averages do | |||
|
|||
subject do | |||
array = [1,1,2,2,2,3,4,5] | |||
array.extend Robut::Plugin::Quiz::Range::Averages | |||
end | |||
|
|||
|
|||
describe "#sum" do | |||
|
|||
it "should return the correct value" do | |||
subject.sum.should == 20 | |||
end | |||
|
|||
end | |||
|
|||
describe "#mean" do | |||
|
|||
it "should return the correct value" do | |||
subject.mean.should == 2.5 | |||
end | |||
|
|||
end | |||
|
|||
describe "#mode" do | |||
|
|||
it "should return the correct value" do | |||
subject.mode.should == 2 | |||
end | |||
|
|||
end | |||
|
|||
end | |||
|
|||
end |