Skip to content

Instantly share code, notes, and snippets.

@eLafo
Last active December 11, 2015 05:58
Show Gist options
  • Save eLafo/4555507 to your computer and use it in GitHub Desktop.
Save eLafo/4555507 to your computer and use it in GitHub Desktop.
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
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
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