class Redstruct::Hash

Class to manipulate redis hashes, modeled after Ruby's Hash class.

Public Instance Methods

[](key) click to toggle source

Returns the value at key @param [#to_s] key the hash key @return [nil, String] the value at key, or nil if nothing

# File lib/redstruct/hash.rb, line 14
def [](key)
  return self.connection.hget(@key, key)
end
[]=(key, value) click to toggle source

Sets or updates the value at key @param [#to_s] key the hash key @param [#to_s] value the new value to set @return [Boolean] true if the field was set (not updated!), false otherwise

# File lib/redstruct/hash.rb, line 31
def []=(key, value)
  set(key, value, overwrite: true)
end
decrement(key, by: 1) click to toggle source

Decrements the value at the given key @param [#to_s] key the hash key @param [Integer, Float] by defaults to 1 @return [Integer, Float] returns the decremented value

# File lib/redstruct/hash.rb, line 86
def decrement(key, by: 1)
  return increment(key, by: -by)
end
empty?() click to toggle source

@return [Boolean] true if the hash contains no elements

# File lib/redstruct/hash.rb, line 91
def empty?
  return !exists?
end
get(*keys) click to toggle source

Returns the value at key @param [Array<#to_s>] keys a list of keys to fetch; can be only one @return [Hash<String, String>] if only one key was passed, then return the value for it; otherwise returns a Ruby hash

where each key in the `keys` is mapped to the value returned by redis
# File lib/redstruct/hash.rb, line 22
def get(*keys)
  return self.connection.hget(@key, keys.first) if keys.size == 1
  return self.connection.mapped_hmget(@key, *keys).reject { |_, v| v.nil? }
end
increment(key, by: 1) click to toggle source

Increments the value at the given key @param [#to_s] key the hash key @param [Integer, Float] by defaults to 1 @return [Integer, Float] returns the incremented value

# File lib/redstruct/hash.rb, line 74
def increment(key, by: 1)
  if by.is_a?(Float)
    self.connection.hincrbyfloat(@key, key, by.to_f).to_f
  else
    self.connection.hincrby(@key, key, by.to_i).to_i
  end
end
key?(key) click to toggle source

Checks if a key has a value @param [#to_s] key the key to check for @return [Boolean] true if the key has a value associated, false otherwise

# File lib/redstruct/hash.rb, line 66
def key?(key)
  return coerce_bool(self.connection.hexists(@key, key))
end
keys() click to toggle source

@return [Array<String>] a list of all hash keys with values associated

# File lib/redstruct/hash.rb, line 103
def keys
  return self.connection.hkeys(@key)
end
remove(*keys) click to toggle source

Removes all items for the given keys @param [Array<#to_s>] keys the list of keys to remove @return [Integer] the number of keys removed

# File lib/redstruct/hash.rb, line 59
def remove(*keys)
  return self.connection.hdel(@key, keys)
end
set(key, value, overwrite: true) click to toggle source

Sets or updates the value at key @param [#to_s] key the hash key @param [#to_s] value the new value to set @return [Boolean] true if the field was set (not updated!), false otherwise

# File lib/redstruct/hash.rb, line 39
def set(key, value, overwrite: true)
  result = if overwrite
    self.connection.hset(@key, key, value)
  else
    self.connection.hsetnx(@key, key, value)
  end

  return coerce_bool(result)
end
size() click to toggle source

@return [Integer] the number of key value pairs stored for this hash

# File lib/redstruct/hash.rb, line 113
def size
  return self.connection.hlen(@key)
end
to_enum(match: '*', count: 10) click to toggle source

Use redis-rb hscan_each method to iterate over particular keys @return [Enumerator] base enumerator to iterate of the namespaced keys

# File lib/redstruct/hash.rb, line 119
def to_enum(match: '*', count: 10)
  return self.connection.hscan_each(@key, match: match, count: count)
end
to_h() click to toggle source

Loads all the hash members in memory NOTE: if the hash is expected to be large, use to_enum @return [Hash<String, String>] all key value pairs stored on redis

# File lib/redstruct/hash.rb, line 98
def to_h
  return self.connection.hgetall(@key)
end
update(hash) click to toggle source

Updates the underlying redis hash using the given Ruby hash's key/value mapping @param [Hash] hash the key/value mapping to use @return [Boolean] true if updated, false otherwise

# File lib/redstruct/hash.rb, line 52
def update(hash)
  coerce_bool(self.connection.mapped_hmset(@key, hash))
end
values() click to toggle source

@return [Array<Strign>] a list of all hash values

# File lib/redstruct/hash.rb, line 108
def values
  return self.connection.hvals(@key)
end