Module: RedisVoteable::Voter::InstanceMethods

Defined in:
lib/redis_voteable/voter.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) clear_vote(voteable) Also known as: unvote

Clears an already done vote on a voteable. Raises a NotVotedError if the voter didn't voted for the voteable.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/redis_voteable/voter.rb', line 91

def clear_vote(voteable)
  check_voteable(voteable)
  
  r = redis.multi do
    redis.srem prefixed("#{class_key(voteable)}:#{DOWN_VOTERS}"), "#{class_key(self)}"
    redis.srem prefixed("#{class_key(self)}:#{DOWN_VOTES}"), "#{class_key(voteable)}"
    redis.srem prefixed("#{class_key(voteable)}:#{UP_VOTERS}"), "#{class_key(self)}"
    redis.srem prefixed("#{class_key(self)}:#{UP_VOTES}"), "#{class_key(voteable)}"
  end
  raise Exceptions::NotVotedError unless r[0] == 1 || r[2] == 1
  true
end

- (Object) clear_vote!(voteable) Also known as: unvote!

Clears an already done vote on a voteable, but doesn't raise an error if the voteable was not voted. It ignores the unvote then.



107
108
109
110
111
112
113
114
# File 'lib/redis_voteable/voter.rb', line 107

def clear_vote!(voteable)
  begin
    clear_vote(voteable)
    return true
  rescue
    return false
  end
end

- (Object) down_vote(voteable)

Down vote a voteable. Raises an AlreadyVotedError if the voter already down voted the voteable. Changes an up vote to a down vote if the the voter already up voted the voteable.



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/redis_voteable/voter.rb', line 65

def down_vote(voteable)
  check_voteable(voteable)

  r = redis.multi do
    redis.srem prefixed("#{class_key(voteable)}:#{UP_VOTERS}"), "#{class_key(self)}"
    redis.srem prefixed("#{class_key(self)}:#{UP_VOTES}"), "#{class_key(voteable)}"
    redis.sadd prefixed("#{class_key(voteable)}:#{DOWN_VOTERS}"), "#{class_key(self)}"
    redis.sadd prefixed("#{class_key(self)}:#{DOWN_VOTES}"), "#{class_key(voteable)}"
  end
  raise Exceptions::AlreadyVotedError.new(false) unless r[2] == 1
  true
end

- (Object) down_vote!(voteable)

Down votes the voteable, but doesn't raise an error if the votelable was already down voted. The vote is simply ignored then.



80
81
82
83
84
85
86
87
# File 'lib/redis_voteable/voter.rb', line 80

def down_vote!(voteable)
  begin
    down_vote(voteable)
    return true
  rescue
    return false
  end
end

- (Object) down_voteables



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/redis_voteable/voter.rb', line 177

def down_voteables
  voteables = redis.smembers prefixed("#{class_key(self)}:#{DOWN_VOTES}")
  voteables.map do |voteable|
    tmp = voteable.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

- (Boolean) down_voted?(voteable)

Returns true if the voter down voted the voteable.



150
151
152
# File 'lib/redis_voteable/voter.rb', line 150

def down_voted?(voteable)
  redis.sismember prefixed("#{class_key(voteable)}:#{DOWN_VOTERS}"), "#{class_key(self)}"
end

- (Object) down_votes

Returns the number of downvotes a voter has cast.



128
129
130
# File 'lib/redis_voteable/voter.rb', line 128

def down_votes()
  redis.scard prefixed("#{class_key(self)}:#{DOWN_VOTES}")
end

- (Object) total_votes

Return the total number of votes a voter has cast.



118
119
120
# File 'lib/redis_voteable/voter.rb', line 118

def total_votes()
  up_votes + down_votes
end

- (Object) up_vote(voteable)

Up vote a voteable. Raises an AlreadyVotedError if the voter already up voted the voteable. Changes a down vote to an up vote if the the voter already down voted the voteable.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/redis_voteable/voter.rb', line 38

def up_vote(voteable)
  check_voteable(voteable)

  r = redis.multi do
    redis.srem prefixed("#{class_key(voteable)}:#{DOWN_VOTERS}"), "#{class_key(self)}"
    redis.srem prefixed("#{class_key(self)}:#{DOWN_VOTES}"), "#{class_key(voteable)}"
    redis.sadd prefixed("#{class_key(voteable)}:#{UP_VOTERS}"), "#{class_key(self)}"
    redis.sadd prefixed("#{class_key(self)}:#{UP_VOTES}"), "#{class_key(voteable)}"
  end
  raise Exceptions::AlreadyVotedError.new(true) unless r[2] == 1
  true
end

- (Object) up_vote!(voteable)

Up votes the voteable, but doesn't raise an error if the votelable was already up voted. The vote is simply ignored then.



53
54
55
56
57
58
59
60
# File 'lib/redis_voteable/voter.rb', line 53

def up_vote!(voteable)
  begin
    up_vote(voteable)
    return true
  rescue
    return false
  end
end

- (Object) up_voteables



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/redis_voteable/voter.rb', line 162

def up_voteables
  voteables = redis.smembers prefixed("#{class_key(self)}:#{UP_VOTES}")
  voteables.map do |voteable|
    tmp = voteable.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

- (Boolean) up_voted?(voteable)

Returns true if the voter up voted the voteable.



145
146
147
# File 'lib/redis_voteable/voter.rb', line 145

def up_voted?(voteable)
  redis.sismember prefixed("#{class_key(voteable)}:#{UP_VOTERS}"), "#{class_key(self)}"
end

- (Object) up_votes

Returns the number of upvotes a voter has cast.



123
124
125
# File 'lib/redis_voteable/voter.rb', line 123

def up_votes()
  redis.scard prefixed("#{class_key(self)}:#{UP_VOTES}")
end

- (Object) vote(voteable, direction)



19
20
21
22
23
24
25
# File 'lib/redis_voteable/voter.rb', line 19

def vote(voteable, direction)
  if direction == :up
    up_vote(voteable)
  elsif direction == :down
    down_vote(voteable)
  end
end

- (Object) vote!(voteable, direction)



27
28
29
30
31
32
33
# File 'lib/redis_voteable/voter.rb', line 27

def vote!(voteable, direction)
  if direction == :up
    up_vote!(voteable)
  elsif direction == :down
    down_vote!(voteable)
  end
end

- (Boolean) vote_value?(voteable)

Returns :up, :down, or nil.



138
139
140
141
142
# File 'lib/redis_voteable/voter.rb', line 138

def vote_value?(voteable)
  return :up   if up_voted?(voteable)
  return :down if down_voted?(voteable)
  return nil
end

- (Object) voteables

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).



158
159
160
# File 'lib/redis_voteable/voter.rb', line 158

def voteables
  up_voteables | down_voteables
end

- (Boolean) voted?(voteable)

Returns true if the voter voted for the voteable.



133
134
135
# File 'lib/redis_voteable/voter.rb', line 133

def voted?(voteable)
  up_voted?(voteable) || down_voted?(voteable)
end