Last active
December 11, 2015 05:58
-
-
Save eLafo/4555507 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Location | |
include SamyRoad::Redis::Objects | |
has_counter: :24_hours | |
end | |
# You can access to the counter's value directly like | |
# location.24_hours_count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PharmacyObserver < ActiveRecord::Observer | |
def after_create(pharmacy) | |
pharmacy.location.24_hours_counter.increment | |
end | |
def after_destroy(model) | |
pharmacy.location.24_hours_counter.decrement | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Sanitas | |
module Redis | |
module Objects | |
def self.included(klass) | |
klass.send(:include, Redis::Objects) | |
klass.extend(ClassMethods) | |
end | |
module ClassMethods | |
def has_counter(name, options = {}) | |
counter_name = name.to_s + "_counter" | |
method_name = name.to_s + "_count" | |
counter(counter_name, options) | |
define_method method_name do | |
send(counter_name).value | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment