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, *fields) ⇒ Object



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

def hdel(key, *fields)
  with_hash_at(key) do |hash|
    orig_size = hash.size
    fields = Array(fields).flatten.map(&:to_s)

    if fields.empty?
      raise Redis::CommandError, "ERR wrong number of arguments for 'hdel' command"
    end

    hash.delete_if { |k, _v| fields.include?(k) }
    orig_size - hash.size
  end
end

#hexists(key, field) ⇒ Object



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

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

#hget(key, field) ⇒ Object



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

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

#hgetall(key) ⇒ Object



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

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

#hincrby(key, field, increment) ⇒ Object



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

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



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

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 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



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

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

#hlen(key) ⇒ Object



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

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

#hmget(key, *fields) ⇒ Object



77
78
79
80
81
82
# File 'lib/mock_redis/hash_methods.rb', line 77

def hmget(key, *fields)
  fields.flatten!

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

#hmset(key, *kvpairs) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mock_redis/hash_methods.rb', line 93

def hmset(key, *kvpairs)
  if key.is_a? Array
    err_msg = 'ERR wrong number of arguments for \'hmset\' command'
    kvpairs = key[1..]
    key = key[0]
  end

  kvpairs.flatten!
  assert_has_args(kvpairs, 'hmset')

  if kvpairs.length.odd?
    raise Redis::CommandError, err_msg || 'ERR wrong number of arguments for \'hmset\' command'
  end

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

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



123
124
125
126
# File 'lib/mock_redis/hash_methods.rb', line 123

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



128
129
130
131
132
133
134
135
136
# File 'lib/mock_redis/hash_methods.rb', line 128

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, *args) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mock_redis/hash_methods.rb', line 138

def hset(key, *args)
  added = 0
  with_hash_at(key) do |hash|
    if args.length == 1 && args[0].is_a?(Hash)
      args = args[0].to_a.flatten
    end

    args.each_slice(2) do |field, value|
      added += 1 unless hash.key?(field.to_s)
      hash[field.to_s] = value.to_s
    end
  end
  added
end

#hsetnx(key, field, value) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/mock_redis/hash_methods.rb', line 153

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

#hvals(key) ⇒ Object



162
163
164
# File 'lib/mock_redis/hash_methods.rb', line 162

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

#mapped_hmget(key, *fields) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/mock_redis/hash_methods.rb', line 84

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



113
114
115
116
117
118
119
120
121
# File 'lib/mock_redis/hash_methods.rb', line 113

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