class Redstruct::Counter

Additional counter operations, using redis string values.

Attributes

default_increment[R]

@return [Integer] the default increment value of the counter, defaults to 1

max[R]

@return [Integer] the default maximum value of the counter, leave nil unless you want the cycle effect

Public Class Methods

new(by: 1, max: nil, **options) click to toggle source

@param [Integer] by the default increment value @param [Integer, nil] max the default max value of the counter, leave nil unless you want cyclical counters

Calls superclass method Redstruct::Struct::new
# File lib/redstruct/counter.rb, line 16
def initialize(by: 1, max: nil, **options)
  super(**options)
  @default_increment = by
  @max = max
end

Public Instance Methods

decrement(by: nil, max: nil) click to toggle source

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). @param [Integer, nil] by defaults to @increment, used to increment the underlying counter @param [Integer, nil] max if non-nil, the counter will loop and start over from 0 when it reaches max @example

pry> counter.decrement(by: 10, max: 5) # returns 0, since 10 % 5 == 0
pry> counter.decrement(by: 9, max: 5) # returns -4

@return [Integer] the updated value

# 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() click to toggle source

@return [Integer] the stored value as an integer

Calls superclass method Redstruct::String#get
# File lib/redstruct/counter.rb, line 23
def get
  return super.to_i
end
getset(value) click to toggle source

@param [#to_i] value the object to store @return [Integer] the old value before setting it

Calls superclass method Redstruct::String#getset
# File lib/redstruct/counter.rb, line 37
def getset(value)
  return super(value.to_i).to_i
end
increment(by: nil, max: nil) click to toggle source

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). @param [Integer, nil] by defaults to @increment, used to increment the underlying counter @param [Integer, nil] max if non-nil, the counter will loop and start over from 0 when it reaches max @example

pry> counter.increment(by: 10, max: 5) # returns 0, since 10 % 5 == 0
pry> counter.increment(by: 9, max: 5) # returns 4

@return [Integer] the updated value

# 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
inspectable_attributes() click to toggle source

@!visibility private

# File lib/redstruct/counter.rb, line 92
def inspectable_attributes
  super.merge(max: @max, by: @default_increment)
end
set(value, **options) click to toggle source

Sets the new value, converting it to int first @see Redstruct::String#set @param [#to_i] value the updated counter value @return [Boolean] True if set, false otherwise

Calls superclass method Redstruct::String#set
# File lib/redstruct/counter.rb, line 31
def set(value, **options)
  super(value.to_i, **options)
end