Module: Redis::Objects::Counters::ClassMethods

Defined in:
lib/redis/objects/counters.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#initialized_countersObject (readonly)

Returns the value of attribute initialized_counters.



18
19
20
# File 'lib/redis/objects/counters.rb', line 18

def initialized_counters
  @initialized_counters
end

Instance Method Details

#counter(name, options = {}) ⇒ Object

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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)
  klass_name = '::' + self.name
  if options[:global]
    instance_eval <<-EndMethods
      def #{name}
        @#{name} ||= Redis::Counter.new(redis_field_key(:#{name}), #{klass_name}.redis, #{klass_name}.redis_objects[:#{name}])
      end
    EndMethods
    class_eval <<-EndMethods
      def #{name}
        self.class.#{name}
      end
    EndMethods
  else
    class_eval <<-EndMethods
      def #{name}
        @#{name} ||= Redis::Counter.new(redis_field_key(:#{name}), #{klass_name}.redis, #{klass_name}.redis_objects[:#{name}])
      end
    EndMethods
  end
end

#decrement_counter(name, id = nil, by = 1, &block) ⇒ Object

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



67
68
69
70
71
72
73
# File 'lib/redis/objects/counters.rb', line 67

def decrement_counter(name, id=nil, by=1, &block)
  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, value, &block) : value
end

#get_counter(name, id = nil) ⇒ Object

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



49
50
51
52
53
# File 'lib/redis/objects/counters.rb', line 49

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) ⇒ Object

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



84
85
86
87
88
# File 'lib/redis/objects/counters.rb', line 84

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) ⇒ Object

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



57
58
59
60
61
62
63
# File 'lib/redis/objects/counters.rb', line 57

def increment_counter(name, id=nil, by=1, &block)
  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, value, &block) : value
end

#reset_counter(name, id = nil, to = nil) ⇒ Object

Reset a counter to its starting value.



76
77
78
79
80
81
# File 'lib/redis/objects/counters.rb', line 76

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