Class: Redpear::Store::Counter

Inherits:
Value show all
Defined in:
lib/redpear/store/counter.rb

Constant Summary

Constants inherited from Base

Base::IS_NIL, Base::IS_ONE, Base::IS_TRUE, Base::IS_ZERO, Base::PICK_FIRST, Base::TO_INT, Base::TO_SET

Instance Attribute Summary

Attributes inherited from Base

#conn, #key

Instance Method Summary collapse

Methods inherited from Value

#==, #append, #nil?, #replace, #respond_to?, #value, #value=

Methods inherited from Base

#clear, #exists?, #expire, #expire_at, #expire_in, #initialize, #inspect, #purge!, temporary, #ttl, #type, #value, #watch

Constructor Details

This class inherits a constructor from Redpear::Store::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Redpear::Store::Value

Instance Method Details

#decrement(by = 1) ⇒ Object Also known as: previous, prev

Decrements the value

Parameters:

  • by (Integer) (defaults to: 1)

    The decrement, defaults to 1



38
39
40
41
42
43
44
45
# File 'lib/redpear/store/counter.rb', line 38

def decrement(by = 1)
  case by
  when 1
    conn.decr(key)
  else
    conn.decrby(key, by)
  end
end

#getInteger

Returns the value.

Returns:

  • (Integer)

    the value



4
5
6
7
8
9
10
11
# File 'lib/redpear/store/counter.rb', line 4

def get
  case value = super
  when Redis::Future
    value.instance_eval { @transformation = TO_INT }
  else
    value.to_i
  end
end

#increment(by = 1) ⇒ Object Also known as: next

Increments the value

Parameters:

  • by (Integer) (defaults to: 1)

    The increment, defaults to 1



25
26
27
28
29
30
31
32
# File 'lib/redpear/store/counter.rb', line 25

def increment(by = 1)
  case by
  when 1
    conn.incr(key)
  else
    conn.incrby(key, by)
  end
end

#set(value) ⇒ Object

Sets the value

Parameters:

  • value (Integer)

    the value to set



15
16
17
# File 'lib/redpear/store/counter.rb', line 15

def set(value)
  super Kernel::Integer(value)
end