Class: ActsAsCaesar::Persistence

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_caesar/objects/persistence.rb

Class Method Summary collapse

Class Method Details

.connectionObject



7
8
9
# File 'lib/acts_as_caesar/objects/persistence.rb', line 7

def connection
  $redis ||= Redis.new(:host => 'localhost', :port => 6379)
end

.decrement_total_votes_on(object) ⇒ Object



17
18
19
20
21
# File 'lib/acts_as_caesar/objects/persistence.rb', line 17

def decrement_total_votes_on(object)
  result = connection.decr("#{object.key}_total")
  Notification.publish(object)
  result
end

.increment_total_votes_on(object) ⇒ Object



11
12
13
14
15
# File 'lib/acts_as_caesar/objects/persistence.rb', line 11

def increment_total_votes_on(object)
  result = connection.incr("#{object.key}_total")
  Notification.publish(object)
  result
end

.set_vote_value(value, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/acts_as_caesar/objects/persistence.rb', line 32

def set_vote_value(value, options = {})
  # load the keys
  key = [options[:candidate], options[:voter]].map(&:key).join("_")
  # if the value was zero, delete it
  if value == 0
    connection.del(key)
  # if the value isn't zero, update it
  else
    connection.set(key, value)
  end
end

.vote_total(object) ⇒ Object



23
24
25
# File 'lib/acts_as_caesar/objects/persistence.rb', line 23

def vote_total(object)
  (connection.get("#{object.key}_total") || 0).to_i
end

.vote_value(options) ⇒ Object



27
28
29
30
# File 'lib/acts_as_caesar/objects/persistence.rb', line 27

def vote_value(options)
  key = [options[:candidate], options[:voter]].map(&:key).join("_")
  (connection.get(key) || 0).to_i
end