Module: MockRedis::HashMethods

Includes:
Assertions, UtilityMethods
Included in:
Database
Defined in:
lib/mock_redis/hash_methods.rb

Instance Method Summary collapse

Instance Method Details

#hdel(key, field) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mock_redis/hash_methods.rb', line 9

def hdel(key, field)
  with_hash_at(key) do |hash|
    if field.is_a?(Array)
      orig_size = hash.size
      fields    = field.map(&:to_s)
      hash.delete_if { |k, _v| fields.include?(k) }
      orig_size - hash.size
    else
      hash.delete(field.to_s) ? 1 : 0
    end
  end
end

#hexists(key, field) ⇒ Object



22
23
24
# File 'lib/mock_redis/hash_methods.rb', line 22

def hexists(key, field)
  with_hash_at(key) { |h| h.key?(field.to_s) }
end

#hget(key, field) ⇒ Object



26
27
28
# File 'lib/mock_redis/hash_methods.rb', line 26

def hget(key, field)
  with_hash_at(key) { |h| h[field.to_s] }
end

#hgetall(key) ⇒ Object



30
31
32
# File 'lib/mock_redis/hash_methods.rb', line 30

def hgetall(key)
  with_hash_at(key) { |h| h }
end

#hincrby(key, field, increment) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mock_redis/hash_methods.rb', line 34

def hincrby(key, field, increment)
  with_hash_at(key) do |hash|
    field = field.to_s
    unless can_incr?(data[key][field])
      raise Redis::CommandError, 'ERR hash value is not an integer'
    end
    unless looks_like_integer?(increment.to_s)
      raise Redis::CommandError, 'ERR value is not an integer or out of range'
    end

    new_value = (hash[field] || '0').to_i + increment.to_i
    hash[field] = new_value.to_s
    new_value
  end
end

#hincrbyfloat(key, field, increment) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mock_redis/hash_methods.rb', line 50

def hincrbyfloat(key, field, increment)
  with_hash_at(key) do |hash|
    field = field.to_s
    unless can_incr_float?(data[key][field])
      raise Redis::CommandError, 'ERR hash value is not a valid float'
    end

    unless looks_like_float?(increment.to_s)
      raise Redis::CommandError, 'ERR value is not a valid float'
    end

    new_value = (hash[field] || '0').to_f + increment.to_f
    new_value = new_value.to_i if new_value % 1 == 0
    hash[field] = new_value.to_s
    new_value
  end
end

#hkeys(key) ⇒ Object



68
69
70
# File 'lib/mock_redis/hash_methods.rb', line 68

def hkeys(key)
  with_hash_at(key, &:keys)
end

#hlen(key) ⇒ Object



72
73
74
# File 'lib/mock_redis/hash_methods.rb', line 72

def hlen(key)
  hkeys(key).length
end

#hmget(key, *fields) ⇒ Object



76
77
78
79
# File 'lib/mock_redis/hash_methods.rb', line 76

def hmget(key, *fields)
  assert_has_args(fields, 'hmget')
  fields.flatten.map { |f| hget(key, f) }
end

#hmset(key, *kvpairs) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mock_redis/hash_methods.rb', line 90

def hmset(key, *kvpairs)
  kvpairs.flatten!
  assert_has_args(kvpairs, 'hmset')
  if kvpairs.length.odd?
    raise Redis::CommandError, 'ERR wrong number of arguments for HMSET'
  end

  kvpairs.each_slice(2) do |(k, v)|
    hset(key, k, v)
  end
  'OK'
end

#hscan(key, cursor, opts = {}) ⇒ Object



113
114
115
116
# File 'lib/mock_redis/hash_methods.rb', line 113

def hscan(key, cursor, opts = {})
  opts = opts.merge(key: lambda { |x| x[0] })
  common_scan(hgetall(key).to_a, cursor, opts)
end

#hscan_each(key, opts = {}, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/mock_redis/hash_methods.rb', line 118

def hscan_each(key, opts = {}, &block)
  return to_enum(:hscan_each, key, opts) unless block_given?
  cursor = 0
  loop do
    cursor, values = hscan(key, cursor, opts)
    values.each(&block)
    break if cursor == '0'
  end
end

#hset(key, field, value) ⇒ Object



128
129
130
131
# File 'lib/mock_redis/hash_methods.rb', line 128

def hset(key, field, value)
  with_hash_at(key) { |h| h[field.to_s] = value.to_s }
  true
end

#hsetnx(key, field, value) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/mock_redis/hash_methods.rb', line 133

def hsetnx(key, field, value)
  if hget(key, field)
    false
  else
    hset(key, field, value)
    true
  end
end

#hvals(key) ⇒ Object



142
143
144
# File 'lib/mock_redis/hash_methods.rb', line 142

def hvals(key)
  with_hash_at(key, &:values)
end

#mapped_hmget(key, *fields) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/mock_redis/hash_methods.rb', line 81

def mapped_hmget(key, *fields)
  reply = hmget(key, *fields)
  if reply.is_a?(Array)
    Hash[fields.zip(reply)]
  else
    reply
  end
end

#mapped_hmset(key, hash) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/mock_redis/hash_methods.rb', line 103

def mapped_hmset(key, hash)
  kvpairs = hash.to_a.flatten
  assert_has_args(kvpairs, 'hmset')
  if kvpairs.length.odd?
    raise Redis::CommandError, "ERR wrong number of arguments for 'hmset' command"
  end

  hmset(key, *kvpairs)
end