Module: RedisVoteable::Voteable::InstanceMethods
- Defined in:
- lib/redis_voteable/voteable.rb
Instance Method Summary (collapse)
-
- (Object) confidence(bound = :lower)
Calculates the (lower) bound of the Wilson confidence interval See: en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval and: www.evanmiller.org/how-not-to-sort-by-average-rating.html.
- - (Object) down_percentage
-
- (Boolean) down_voted?(voter)
Returns true if the voter down voted the voteable.
- - (Object) down_voters
- - (Object) down_votes
-
- (Object) tally
Return the difference between up and and votes.
- - (Object) total_votes
- - (Object) up_percentage
-
- (Boolean) up_voted?(voter)
Returns true if the voter up voted the voteable.
- - (Object) up_voters
- - (Object) up_votes
-
- (Boolean) vote_value?(voter)
Returns :up, :down, or nil.
-
- (Boolean) voted?(voter)
Returns true if the voter voted on the voteable.
-
- (Object) voters
Returns an array of objects that are voters that voted on this voteable.
Instance Method Details
- (Object) confidence(bound = :lower)
Calculates the (lower) bound of the Wilson confidence interval See: en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval and: www.evanmiller.org/how-not-to-sort-by-average-rating.html
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/redis_voteable/voteable.rb', line 114 def confidence(bound = :lower) #include Math n = up_votes + down_votes if n == 0 return 0 if n == 0 end z = 1.4395314800662002 # Determines confidence to estimate. # 1.0364333771448913 = 70% # 1.2815515594600038 = 80% # 1.4395314800662002 = 85% # 1.644853646608357 = 90% # 1.9599639715843482 = 95% # 2.2414027073522136 = 97.5% p_hat = 1.0*up_votes/n left = p_hat + z*z/(2*n) right = z * Math.sqrt( (p_hat*(1-p_hat) + z*z/(4*n)) / n ) under = 1 + z*z/n return (left - right) / under unless bound == :upper return (left + right) / under #return Math.sqrt( p_hat + z * z / ( 2 * n ) - z * ( ( p_hat * ( 1 - p_hat ) + z * z / ( 4 * n ) ) / n ) ) / ( 1 + z * z / n ) end |
- (Object) down_percentage
45 46 47 48 |
# File 'lib/redis_voteable/voteable.rb', line 45 def down_percentage return (down_votes.to_f * 100 / total_votes) unless total_votes == 0 nil end |
- (Boolean) down_voted?(voter)
Returns true if the voter down voted the voteable.
68 69 70 |
# File 'lib/redis_voteable/voteable.rb', line 68 def down_voted?(voter) redis.sismember prefixed("#{class_key(voter)}:#{DOWN_VOTES}"), "#{class_key(self)}" end |
- (Object) down_voters
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/redis_voteable/voteable.rb', line 96 def down_voters voters = redis.smembers prefixed("#{class_key(self)}:#{DOWN_VOTERS}") voters.map do |voter| tmp = voter.split(':') klass = tmp[0, tmp.length-1].join(':').constantize if klass.respond_to?('find') klass.find(tmp.last) elsif klass.respond_to?('get') klass.get(tmp.last) else nil end end end |
- (Object) down_votes
26 27 28 |
# File 'lib/redis_voteable/voteable.rb', line 26 def down_votes redis.scard prefixed("#{class_key(self)}:#{DOWN_VOTERS}") end |
- (Object) tally
Return the difference between up and and votes. May be negative if there are more down than up votes.
36 37 38 |
# File 'lib/redis_voteable/voteable.rb', line 36 def tally up_votes - down_votes end |
- (Object) total_votes
30 31 32 |
# File 'lib/redis_voteable/voteable.rb', line 30 def total_votes up_votes + down_votes end |
- (Object) up_percentage
40 41 42 43 |
# File 'lib/redis_voteable/voteable.rb', line 40 def up_percentage return (up_votes.to_f * 100 / total_votes) unless total_votes == 0 nil end |
- (Boolean) up_voted?(voter)
Returns true if the voter up voted the voteable.
63 64 65 |
# File 'lib/redis_voteable/voteable.rb', line 63 def up_voted?(voter) redis.sismember prefixed("#{class_key(voter)}:#{UP_VOTES}"), "#{class_key(self)}" end |
- (Object) up_voters
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/redis_voteable/voteable.rb', line 81 def up_voters voters = redis.smembers prefixed("#{class_key(self)}:#{UP_VOTERS}") voters.map do |voter| tmp = voter.split(':') klass = tmp[0, tmp.length-1].join(':').constantize if klass.respond_to?('find') klass.find(tmp.last) elsif klass.respond_to?('get') klass.get(tmp.last) else nil end end end |
- (Object) up_votes
22 23 24 |
# File 'lib/redis_voteable/voteable.rb', line 22 def up_votes redis.scard prefixed("#{class_key(self)}:#{UP_VOTERS}") end |
- (Boolean) vote_value?(voter)
Returns :up, :down, or nil.
56 57 58 59 60 |
# File 'lib/redis_voteable/voteable.rb', line 56 def vote_value?(voter) return :up if up_voted?(voter) return :down if down_voted?(voter) return nil end |
- (Boolean) voted?(voter)
Returns true if the voter voted on the voteable.
51 52 53 |
# File 'lib/redis_voteable/voteable.rb', line 51 def voted?(voter) up_voted?(voter) || down_voted?(voter) end |
- (Object) voters
Returns an array of objects that are voters that voted on this voteable. This method can be very slow, as it constructs each object. Also, it assumes that each object has a find(id) method defined (e.g., any ActiveRecord object).
77 78 79 |
# File 'lib/redis_voteable/voteable.rb', line 77 def voters up_voters | down_voters end |