Class: ActsAsCaesar::Persistence
- Inherits:
-
Object
- Object
- ActsAsCaesar::Persistence
- Defined in:
- lib/acts_as_caesar/objects/persistence.rb
Class Method Summary collapse
- .connection ⇒ Object
- .decrement_total_votes_on(object) ⇒ Object
- .increment_total_votes_on(object) ⇒ Object
- .set_vote_value(value, options = {}) ⇒ Object
- .vote_total(object) ⇒ Object
- .vote_value(options) ⇒ Object
Class Method Details
.connection ⇒ Object
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, = {}) # load the keys key = [[:candidate], [: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() key = [[:candidate], [:voter]].map(&:key).join("_") (connection.get(key) || 0).to_i end |