volleyball 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 0.1.1 / 2010-06-03
2
+
3
+ * starting point
4
+
5
+
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,7 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/volleyball.rb
6
+ rails/init.rb
7
+ test/test_volleyball.rb
data/README.txt ADDED
@@ -0,0 +1,71 @@
1
+ = Vollyball
2
+
3
+ * http://www.bitbucket.org/digger250/vollyball
4
+
5
+ == DESCRIPTION:
6
+
7
+ A small library for yes/no voting on ActiveRecord models.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * None yet
12
+
13
+ == SYNOPSIS:
14
+
15
+ voteable
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * Must have a model called user
20
+
21
+ == INSTALL:
22
+
23
+ * gem install vollball
24
+ * Create a new rails migration and add the following self.up and self.down methods
25
+
26
+ def self.up
27
+ create_table "votes", :force => true do |t|
28
+ t.boolean :up, {:null=>false}
29
+ t.string :subject_type, {:null=>false}
30
+ t.integer :subject_id, {:null=>false}
31
+ t.integer :user_id, {:null=>false}
32
+ t.timestamps
33
+ end
34
+
35
+ add_index "votes", ["user_id"], :name => "fk_votes_user"
36
+ end
37
+
38
+ def self.down
39
+ drop_table :votes
40
+ end
41
+
42
+
43
+ * Add yes_votes/no_votes columns for vote rollup and score on the table you count votes for:
44
+ add_column :my_objects, :yes_votes, :integer, {:default => 0, :null=>false}
45
+ add_column :my_objects, :no_votes, :integer, {:default => 0, :null=>false}
46
+ add_column :my_objects, :score, :float
47
+
48
+ == LICENSE:
49
+
50
+ (The MIT License)
51
+
52
+ Copyright (c) 2010 Justin Coyne
53
+
54
+ Permission is hereby granted, free of charge, to any person obtaining
55
+ a copy of this software and associated documentation files (the
56
+ 'Software'), to deal in the Software without restriction, including
57
+ without limitation the rights to use, copy, modify, merge, publish,
58
+ distribute, sublicense, and/or sell copies of the Software, and to
59
+ permit persons to whom the Software is furnished to do so, subject to
60
+ the following conditions:
61
+
62
+ The above copyright notice and this permission notice shall be
63
+ included in all copies or substantial portions of the Software.
64
+
65
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
66
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
67
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
68
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
69
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
70
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
71
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ # -*- ruby -*-
2
+
3
+
4
+ require 'rake/clean'
5
+ require 'rubygems'
6
+ require 'hoe'
7
+ CLEAN.include %w(**/*.orig)
8
+ Hoe.plugins.delete :rubyforge
9
+ Hoe.plugin :hg
10
+
11
+ Hoe.spec 'volleyball' do
12
+ developer('Justin Coyne', 'jcoyne@justincoyne.com')
13
+ extra_deps << %w(statistics2 >=0.54)
14
+ extra_dev_deps << %w(shoulda >=2.10.0) << %w(mocha >=0.9.8) << %w(active_support)
15
+ end
16
+
17
+ task :install =>:package do
18
+ $:<< 'lib'
19
+ require 'volleyball'
20
+ puts `GEM_HOME=~/.gem/ruby/1.8 gem install pkg/volleyball-#{Volleyball::VERSION}.gem`
21
+ end
22
+ # vim: syntax=ruby
23
+ #
data/lib/volleyball.rb ADDED
@@ -0,0 +1,45 @@
1
+ require 'statistics2'
2
+ module Volleyball
3
+ VERSION = "0.1.2"
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+
8
+ end
9
+
10
+ module ClassMethods
11
+ def votable (args={})
12
+ has_many :votes, :as=>:subject
13
+ before_save :calculate_score
14
+ cattr_accessor :yes_adjustment
15
+ cattr_accessor :no_adjustment
16
+ self.yes_adjustment = args[:yes_start] ? args[:yes_start] : 0
17
+ self.no_adjustment = args[:no_start] ? args[:no_start] : 0
18
+
19
+ include Volleyball::InstanceMethods
20
+ end
21
+ end
22
+
23
+ module InstanceMethods
24
+ def calculate_score
25
+ self.score = (rank * 100).floor.to_f/10
26
+ end
27
+
28
+ def rank
29
+ adjusted_yes_votes = self.yes_votes + self.class.yes_adjustment
30
+ adjusted_total_votes = self.no_votes + adjusted_yes_votes + self.class.no_adjustment
31
+ ci_lower_bound(adjusted_yes_votes, adjusted_total_votes, 0.10)
32
+ end
33
+
34
+ # This is the Wilson Confidence Interval
35
+ # http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
36
+ def ci_lower_bound(pos, n, power)
37
+ return 0 if n == 0
38
+ z = Statistics2.normaldist(1-power/2)
39
+ phat = 1.0*pos/n
40
+ (phat + z*z/(2*n) - z * Math.sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)
41
+ end
42
+ end
43
+
44
+ end
45
+
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'volleyball'
2
+ ActiveRecord::Base.send(:include, Volleyball)
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+ require "test/unit"
3
+ require 'volleyball'
4
+ require 'shoulda'
5
+ require 'mocha'
6
+ require 'ruby-debug'
7
+ require 'active_support' #for cattr_accessor
8
+ class TestVolleyball < Test::Unit::TestCase
9
+
10
+ class MockArModel
11
+ include Volleyball
12
+ end
13
+
14
+ context "volleyball lib" do
15
+
16
+ setup do
17
+ MockArModel.expects(:has_many).with(:votes, {:as => :subject})
18
+ MockArModel.expects(:before_save).with(:calculate_score)
19
+ MockArModel.any_instance.stubs(:yes_votes).returns(200)
20
+ MockArModel.any_instance.stubs(:no_votes).returns(121)
21
+ MockArModel.votable :yes_start=>7, :no_start=>5
22
+ end
23
+ should "have yes_adjustment" do
24
+ assert_equal 7, MockArModel.yes_adjustment
25
+ end
26
+ should "have no_adjustment" do
27
+ assert_equal 5, MockArModel.no_adjustment
28
+ end
29
+
30
+ should "Calculate score" do
31
+ instance = MockArModel.new
32
+ instance.expects(:score=).with(5.9)
33
+ assert instance.calculate_score
34
+ end
35
+
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: volleyball
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
11
+ platform: ruby
12
+ authors:
13
+ - Justin Coyne
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-03 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: statistics2
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 103
30
+ segments:
31
+ - 0
32
+ - 54
33
+ version: "0.54"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: shoulda
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 39
45
+ segments:
46
+ - 2
47
+ - 10
48
+ - 0
49
+ version: 2.10.0
50
+ type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: mocha
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 43
61
+ segments:
62
+ - 0
63
+ - 9
64
+ - 8
65
+ version: 0.9.8
66
+ type: :development
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: active_support
70
+ prerelease: false
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ type: :development
81
+ version_requirements: *id004
82
+ - !ruby/object:Gem::Dependency
83
+ name: hoe
84
+ prerelease: false
85
+ requirement: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 21
91
+ segments:
92
+ - 2
93
+ - 6
94
+ - 1
95
+ version: 2.6.1
96
+ type: :development
97
+ version_requirements: *id005
98
+ description: A small library for yes/no voting on ActiveRecord models.
99
+ email:
100
+ - jcoyne@justincoyne.com
101
+ executables: []
102
+
103
+ extensions: []
104
+
105
+ extra_rdoc_files:
106
+ - History.txt
107
+ - Manifest.txt
108
+ - README.txt
109
+ files:
110
+ - History.txt
111
+ - Manifest.txt
112
+ - README.txt
113
+ - Rakefile
114
+ - lib/volleyball.rb
115
+ - rails/init.rb
116
+ - test/test_volleyball.rb
117
+ has_rdoc: true
118
+ homepage: http://www.bitbucket.org/digger250/vollyball
119
+ licenses: []
120
+
121
+ post_install_message:
122
+ rdoc_options:
123
+ - --main
124
+ - README.txt
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: 3
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ hash: 3
142
+ segments:
143
+ - 0
144
+ version: "0"
145
+ requirements: []
146
+
147
+ rubyforge_project: volleyball
148
+ rubygems_version: 1.3.7
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: A small library for yes/no voting on ActiveRecord models.
152
+ test_files:
153
+ - test/test_volleyball.rb