Class: Redstruct::Types::Hash

Inherits:
Struct show all
Includes:
Utils::Coercion
Defined in:
lib/redstruct/types/hash.rb

Instance Attribute Summary

Attributes inherited from Base

#key

Instance Method Summary collapse

Methods included from Utils::Coercion

coerce_array, coerce_bool

Methods inherited from Struct

#exists?, #expire, #expire_at, #inspectable_attributes, #persist, #type

Methods included from Utils::Inspectable

#inspect, #inspectable_attributes, #to_s

Methods inherited from Base

#initialize, #inspectable_attributes, #with

Constructor Details

This class inherits a constructor from Redstruct::Types::Base

Instance Method Details

#[](key) ⇒ Object



6
7
8
# File 'lib/redstruct/types/hash.rb', line 6

def [](key)
  return self.connection.hget(@key, key)
end

#[]=(key, value) ⇒ Object



10
11
12
# File 'lib/redstruct/types/hash.rb', line 10

def []=(key, value)
  self.connection.hset(@key, key, value)
end

#decr(key, increment: 1) ⇒ Object



47
48
49
# File 'lib/redstruct/types/hash.rb', line 47

def decr(key, increment: 1)
  return incr(key, (-increment))
end

#delete(*keys) ⇒ Object



31
32
33
# File 'lib/redstruct/types/hash.rb', line 31

def delete(*keys)
  return self.connection.hdel(@key, keys)
end

#each(options = {}, &block) ⇒ Object



67
68
69
# File 'lib/redstruct/types/hash.rb', line 67

def each(options = {}, &block)
  return self.connection.hscan_each(@key, options, &block)
end

#get(*keys) ⇒ Object



22
23
24
25
# File 'lib/redstruct/types/hash.rb', line 22

def get(*keys)
  return self[keys.first] if keys.size == 1
  return self.connection.mapped_hmget(@key, *keys)
end

#incr(key, increment: 1) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/redstruct/types/hash.rb', line 39

def incr(key, increment: 1)
  if increment.is_a?(Float)
    self.connection.hincrbyfloat(@key, key, increment.to_f)
  else
    self.connection.hincrby(@key, key, increment)
  end
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/redstruct/types/hash.rb', line 35

def key?(key)
  return coerce_bool(self.connection.hexists(@key, key))
end

#keysObject



55
56
57
# File 'lib/redstruct/types/hash.rb', line 55

def keys
  return self.connection.hkeys(@key)
end

#set(key, value, overwrite: true) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/redstruct/types/hash.rb', line 14

def set(key, value, overwrite: true)
  if overwrite
    self[key] = value
  else
    self.connection.hsetnx(@key, key, value)
  end
end

#sizeObject



63
64
65
# File 'lib/redstruct/types/hash.rb', line 63

def size
  return self.connection.hlen(@key)
end

#to_hObject



51
52
53
# File 'lib/redstruct/types/hash.rb', line 51

def to_h
  return self.connection.hgetall(@key)
end

#update(hash) ⇒ Object



27
28
29
# File 'lib/redstruct/types/hash.rb', line 27

def update(hash)
  self.connection.mapped_hmset(@key, hash)
end

#valuesObject



59
60
61
# File 'lib/redstruct/types/hash.rb', line 59

def values
  return self.connection.hvals(@key)
end