module Redis::Objects::Counters::ClassMethods

Class methods that appear in your class when you include Redis::Objects.

Attributes

initialized_counters[R]

Public Instance Methods

counter(name, options={}) click to toggle source

Define a new counter. It will function like a regular instance method, so it can be used alongside ActiveRecord, DataMapper, etc.

# File lib/redis/objects/counters.rb, line 22
def counter(name, options={})
  options[:start] ||= 0
  options[:type]  ||= options[:start] == 0 ? :increment : :decrement
  redis_objects[name.to_sym] = options.merge(:type => :counter)
  ivar_name = :"@#{name}"

  mod = Module.new do
    define_method(name) do
      instance_variable_get(ivar_name) or
        instance_variable_set(ivar_name,
          Redis::Counter.new(
            redis_field_key(name), redis_field_redis(name), redis_options(name)
          )
        )
    end
  end

  if options[:global]
    extend mod

    # dispatch to class methods
    define_method(name) do
      self.class.public_send(name)
    end
  else
    include mod
  end
end
decrement_counter(name, id=nil, by=1, &block) click to toggle source

Decrement a counter with the specified name and id. Accepts a block like the instance method. See Redis::Objects::Counter for details.

Calls superclass method
# File lib/redis/objects/counters.rb, line 72
def decrement_counter(name, id=nil, by=1, &block)
  name = name.to_sym
  return super(name, id) unless counter_defined?(name)
  verify_counter_defined!(name, id)
  initialize_counter!(name, id)
  value = redis.decrby(redis_field_key(name, id), by).to_i
  block_given? ? rewindable_block(:increment_counter, name, id, by, value, &block) : value
end
get_counter(name, id=nil) click to toggle source

Get the current value of the counter. It is more efficient to use the instance method if possible.

# File lib/redis/objects/counters.rb, line 53
def get_counter(name, id=nil)
  verify_counter_defined!(name, id)
  initialize_counter!(name, id)
  redis.get(redis_field_key(name, id)).to_i
end
getset_counter(name, id=nil, to=nil) click to toggle source

Set a counter to its starting value and return the old value.

# File lib/redis/objects/counters.rb, line 90
def getset_counter(name, id=nil, to=nil)
  verify_counter_defined!(name, id)
  to = redis_objects[name][:start] if to.nil?
  redis.getset(redis_field_key(name, id), to.to_i).to_i
end
increment_counter(name, id=nil, by=1, &block) click to toggle source

Increment a counter with the specified name and id. Accepts a block like the instance method. See Redis::Objects::Counter for details.

Calls superclass method
# File lib/redis/objects/counters.rb, line 61
def increment_counter(name, id=nil, by=1, &block)
  name = name.to_sym
  return super(name, id) unless counter_defined?(name)
  verify_counter_defined!(name, id)
  initialize_counter!(name, id)
  value = redis.incrby(redis_field_key(name, id), by).to_i
  block_given? ? rewindable_block(:decrement_counter, name, id, by, value, &block) : value
end
reset_counter(name, id=nil, to=nil) click to toggle source

Reset a counter to its starting value.

# File lib/redis/objects/counters.rb, line 82
def reset_counter(name, id=nil, to=nil)
  verify_counter_defined!(name, id)
  to = redis_objects[name][:start] if to.nil?
  redis.set(redis_field_key(name, id), to.to_i)
  true
end