Class: Redstruct::Counter
- Inherits:
-
String
- Object
- Factory::Object
- Struct
- String
- Redstruct::Counter
- Defined in:
- lib/redstruct/counter.rb
Overview
Additional counter operations, using redis string values.
Instance Attribute Summary collapse
-
#default_increment ⇒ Integer
readonly
The default increment value of the counter, defaults to 1.
-
#max ⇒ Integer
readonly
The default maximum value of the counter, leave nil unless you want the cycle effect.
Attributes inherited from Struct
Attributes inherited from Factory::Object
Instance Method Summary collapse
-
#decrement(by: nil, max: nil) ⇒ Integer
Decrements the counter by the given value.
-
#get ⇒ Integer
The stored value as an integer.
-
#getset(value) ⇒ Integer
The old value before setting it.
-
#increment(by: nil, max: nil) ⇒ Integer
Increments the counter by the given value.
-
#initialize(by: 1, max: nil, **options) ⇒ Counter
constructor
A new instance of Counter.
-
#set(value, **options) ⇒ Boolean
Sets the new value, converting it to int first.
Methods inherited from String
#delete_if_equals, #delete_if_equals_script, #length, #slice
Methods included from Utils::Scriptable
Methods inherited from Struct
#delete, #dump, #exists?, #expire, #expire_at, #persist, #restore, #ttl, #type
Methods included from Utils::Coercion
Methods inherited from Factory::Object
Methods included from Utils::Inspectable
Constructor Details
#initialize(by: 1, max: nil, **options) ⇒ Counter
Returns a new instance of Counter.
16 17 18 19 20 |
# File 'lib/redstruct/counter.rb', line 16 def initialize(by: 1, max: nil, **) super(**) @default_increment = by @max = max end |
Instance Attribute Details
#default_increment ⇒ Integer (readonly)
Returns the default increment value of the counter, defaults to 1.
9 10 11 |
# File 'lib/redstruct/counter.rb', line 9 def default_increment @default_increment end |
#max ⇒ Integer (readonly)
Returns the default maximum value of the counter, leave nil unless you want the cycle effect.
12 13 14 |
# File 'lib/redstruct/counter.rb', line 12 def max @max end |
Instance Method Details
#decrement(by: nil, max: nil) ⇒ Integer
Decrements the counter by the given value. If max is given, will loop around and start again from 0 (will decrement by (current_value + by) % max).
68 69 70 71 72 73 74 75 76 |
# File 'lib/redstruct/counter.rb', line 68 def decrement(by: nil, max: nil) by ||= @default_increment by = -by.to_i max ||= @max max = -max unless max.nil? return increment(by: by, max: max) end |
#get ⇒ Integer
Returns the stored value as an integer.
23 24 25 |
# File 'lib/redstruct/counter.rb', line 23 def get return super.to_i end |
#getset(value) ⇒ Integer
Returns the old value before setting it.
37 38 39 |
# File 'lib/redstruct/counter.rb', line 37 def getset(value) return super(value.to_i).to_i end |
#increment(by: nil, max: nil) ⇒ Integer
Increments the counter by the given value. If max is given, will loop around and start again from 0 (will increment by (current_value + by) % max).
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/redstruct/counter.rb', line 48 def increment(by: nil, max: nil) by ||= @default_increment max ||= @max value = if max.nil? self.connection.incrby(@key, by.to_i).to_i else ring_increment_script(keys: @key, argv: [by.to_i, max.to_i]).to_i end return value end |
#set(value, **options) ⇒ Boolean
Sets the new value, converting it to int first
31 32 33 |
# File 'lib/redstruct/counter.rb', line 31 def set(value, **) super(value.to_i, **) end |