Module: MockRedis::ListMethods

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

Instance Method Summary collapse

Instance Method Details

#blpop(*args) ⇒ Object



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

def blpop(*args)
  lists, timeout = extract_timeout(args)
  nonempty_list = first_nonempty_list(lists)

  if nonempty_list
    [nonempty_list, lpop(nonempty_list)]
  elsif timeout > 0
    nil
  else
    raise MockRedis::WouldBlock, "Can't block forever"
  end
end

#brpop(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mock_redis/list_methods.rb', line 22

def brpop(*args)
  lists, timeout = extract_timeout(args)
  nonempty_list = first_nonempty_list(lists)

  if nonempty_list
    [nonempty_list, rpop(nonempty_list)]
  elsif timeout > 0
    nil
  else
    raise MockRedis::WouldBlock, "Can't block forever"
  end
end

#brpoplpush(source, destination, timeout) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mock_redis/list_methods.rb', line 35

def brpoplpush(source, destination, timeout)
  assert_valid_timeout(timeout)

  if llen(source) > 0
    rpoplpush(source, destination)
  elsif timeout > 0
    nil
  else
    raise MockRedis::WouldBlock, "Can't block forever"
  end
end

#lindex(key, index) ⇒ Object



47
48
49
# File 'lib/mock_redis/list_methods.rb', line 47

def lindex(key, index)
  with_list_at(key) {|l| l[index]}
end

#linsert(key, position, pivot, value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mock_redis/list_methods.rb', line 51

def linsert(key, position, pivot, value)
  unless %w[before after].include?(position.to_s)
    raise RuntimeError, "ERR syntax error"
  end

  assert_listy(key)
  return 0 unless data[key]

  pivot_position = (0..llen(key) - 1).find do |i|
    data[key][i] == pivot.to_s
  end

  return -1 unless pivot_position

  insertion_index = if position.to_s == 'before'
                      pivot_position
                    else
                      pivot_position + 1
                    end

  data[key].insert(insertion_index, value.to_s)
  llen(key)
end

#llen(key) ⇒ Object



75
76
77
# File 'lib/mock_redis/list_methods.rb', line 75

def llen(key)
  with_list_at(key, &:length)
end

#lpop(key) ⇒ Object



79
80
81
# File 'lib/mock_redis/list_methods.rb', line 79

def lpop(key)
  with_list_at(key, &:shift)
end

#lpush(key, value) ⇒ Object



83
84
85
86
# File 'lib/mock_redis/list_methods.rb', line 83

def lpush(key, value)
  with_list_at(key) {|l| l.unshift(value.to_s)}
  llen(key)
end

#lpushx(key, value) ⇒ Object



88
89
90
91
92
# File 'lib/mock_redis/list_methods.rb', line 88

def lpushx(key, value)
  assert_listy(key)
  return 0 unless list_at?(key)
  lpush(key, value)
end

#lrange(key, start, stop) ⇒ Object



94
95
96
# File 'lib/mock_redis/list_methods.rb', line 94

def lrange(key, start, stop)
  with_list_at(key) {|l| l[start..stop]}
end

#lrem(key, count, value) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mock_redis/list_methods.rb', line 98

def lrem(key, count, value)
  count = count.to_i
  value = value.to_s

  with_list_at(key) do |list|
    indices_with_value = (0..(llen(key) - 1)).find_all do |i|
      list[i] == value
    end

    indices_to_delete = if count == 0
                          indices_with_value.reverse
                        elsif count > 0
                          indices_with_value.take(count).reverse
                        else
                          indices_with_value.reverse.take(-count)
                        end

    indices_to_delete.each {|i| list.delete_at(i)}.length
  end
end

#lset(key, index, value) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mock_redis/list_methods.rb', line 119

def lset(key, index, value)
  assert_listy(key)

  unless list_at?(key)
    raise RuntimeError, "ERR no such key"
  end

  unless (0...llen(key)).include?(index)
    raise RuntimeError, "ERR index out of range"
  end

  data[key][index] = value.to_s
  'OK'
end

#ltrim(key, start, stop) ⇒ Object



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

def ltrim(key, start, stop)
  with_list_at(key) do |list|
    list.replace(list[start..stop] || []) if list
    'OK'
  end
end

#rpop(key) ⇒ Object



141
142
143
# File 'lib/mock_redis/list_methods.rb', line 141

def rpop(key)
  with_list_at(key) {|list| list.pop if list}
end

#rpoplpush(source, destination) ⇒ Object



145
146
147
148
149
# File 'lib/mock_redis/list_methods.rb', line 145

def rpoplpush(source, destination)
  value = rpop(source)
  lpush(destination, value)
  value
end

#rpush(key, value) ⇒ Object



151
152
153
154
# File 'lib/mock_redis/list_methods.rb', line 151

def rpush(key, value)
  with_list_at(key) {|l| l.push(value.to_s)}
  llen(key)
end

#rpushx(key, value) ⇒ Object



156
157
158
159
160
# File 'lib/mock_redis/list_methods.rb', line 156

def rpushx(key, value)
  assert_listy(key)
  return 0 unless list_at?(key)
  rpush(key, value)
end