Module: MockRedis::ListMethods
- Includes:
- Assertions, UtilityMethods
- Included in:
- Database
- Defined in:
- lib/mock_redis/list_methods.rb
Instance Method Summary collapse
- #blmove(source, destination, wherefrom, whereto, options = {}) ⇒ Object
- #blpop(*args) ⇒ Object
- #brpop(*args) ⇒ Object
- #brpoplpush(source, destination, timeout: 0) ⇒ Object
- #lindex(key, index) ⇒ Object
- #linsert(key, position, pivot, value) ⇒ Object
- #llen(key) ⇒ Object
- #lmove(source, destination, wherefrom, whereto) ⇒ Object
- #lmpop(*keys, **options) ⇒ Object
- #lpop(key, count = nil) ⇒ Object
- #lpush(key, values) ⇒ Object
- #lpushx(key, value) ⇒ Object
- #lrange(key, start, stop) ⇒ Object
- #lrem(key, count, value) ⇒ Object
- #lset(key, index, value) ⇒ Object
- #ltrim(key, start, stop) ⇒ Object
- #rpop(key, count = nil) ⇒ Object
- #rpoplpush(source, destination) ⇒ Object
- #rpush(key, values) ⇒ Object
- #rpushx(key, value) ⇒ Object
Instance Method Details
#blmove(source, destination, wherefrom, whereto, options = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/mock_redis/list_methods.rb', line 9 def blmove(source, destination, wherefrom, whereto, = {}) = { :timeout => } if .is_a?(Integer) timeout = .is_a?(Hash) && [:timeout] || 0 assert_valid_timeout(timeout) if llen(source) > 0 lmove(source, destination, wherefrom, whereto) elsif timeout > 0 nil else raise MockRedis::WouldBlock, "Can't block forever" end end |
#blpop(*args) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mock_redis/list_methods.rb', line 23 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
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mock_redis/list_methods.rb', line 36 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: 0) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mock_redis/list_methods.rb', line 49 def brpoplpush(source, destination, timeout: 0) 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
61 62 63 |
# File 'lib/mock_redis/list_methods.rb', line 61 def lindex(key, index) with_list_at(key) { |l| l[index.to_i] } end |
#linsert(key, position, pivot, value) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mock_redis/list_methods.rb', line 65 def linsert(key, position, pivot, value) unless %w[before after].include?(position.to_s) raise Error.command_error('ERR syntax error', self) 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
89 90 91 |
# File 'lib/mock_redis/list_methods.rb', line 89 def llen(key) with_list_at(key, &:length) end |
#lmove(source, destination, wherefrom, whereto) ⇒ 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 lmove(source, destination, wherefrom, whereto) assert_listy(source) assert_listy(destination) wherefrom = wherefrom.to_s.downcase whereto = whereto.to_s.downcase assert_where_field(wherefrom, 'where_source') assert_where_field(whereto, 'where_destination') value = wherefrom == 'left' ? lpop(source) : rpop(source) (whereto == 'left' ? lpush(destination, value) : rpush(destination, value)) unless value.nil? value end |
#lmpop(*keys, **options) ⇒ Object
93 94 95 96 97 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 93 def lmpop(*keys, **) keys.each do |key| assert_listy(key) end modifier = .is_a?(Hash) && [:modifier]&.to_s&.downcase || 'left' count = (.is_a?(Hash) && [:count]) || 1 unless %w[left right].include?(modifier) raise Redis::CommandError, 'ERR syntax error' end keys.each do |key| record_count = llen(key) next if record_count.zero? values = [count, record_count].min.times.map do modifier == 'left' ? with_list_at(key, &:shift) : with_list_at(key, &:pop) end return [key, values] end nil end |
#lpop(key, count = nil) ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/mock_redis/list_methods.rb', line 134 def lpop(key, count = nil) return with_list_at(key, &:shift) if count.nil? record_count = llen(key) return nil if record_count.zero? [record_count, count].min.times.map { with_list_at(key, &:shift) } end |
#lpush(key, values) ⇒ Object
143 144 145 146 147 148 |
# File 'lib/mock_redis/list_methods.rb', line 143 def lpush(key, values) values = [values] unless values.is_a?(Array) assert_has_args(values, 'lpush') with_list_at(key) { |l| values.each { |v| l.unshift(v.to_s) } } llen(key) end |
#lpushx(key, value) ⇒ Object
150 151 152 153 154 155 156 157 158 |
# File 'lib/mock_redis/list_methods.rb', line 150 def lpushx(key, value) value = [value] unless value.is_a?(Array) if value.empty? raise Error.command_error("ERR wrong number of arguments for 'lpushx' command", self) end assert_listy(key) return 0 unless list_at?(key) lpush(key, value) end |
#lrange(key, start, stop) ⇒ Object
160 161 162 163 |
# File 'lib/mock_redis/list_methods.rb', line 160 def lrange(key, start, stop) start = start.to_i with_list_at(key) { |l| start < l.size ? l[[start, -l.length].max..stop.to_i] : [] } end |
#lrem(key, count, value) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/mock_redis/list_methods.rb', line 165 def lrem(key, count, value) count = Integer(count) 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
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/mock_redis/list_methods.rb', line 186 def lset(key, index, value) assert_listy(key) unless list_at?(key) raise Error.command_error('ERR no such key', self) end index = index.to_i unless (0...llen(key)).cover?(index) raise Error.command_error('ERR index out of range', self) end data[key][index] = value.to_s 'OK' end |
#ltrim(key, start, stop) ⇒ Object
202 203 204 205 206 207 |
# File 'lib/mock_redis/list_methods.rb', line 202 def ltrim(key, start, stop) with_list_at(key) do |list| list&.replace(list[[start.to_i, -list.length].max..stop.to_i] || []) 'OK' end end |
#rpop(key, count = nil) ⇒ Object
209 210 211 212 213 214 215 216 |
# File 'lib/mock_redis/list_methods.rb', line 209 def rpop(key, count = nil) return with_list_at(key) { |list| list&.pop } if count.nil? record_count = llen(key) return nil if record_count.zero? [record_count, count].min.times.map { with_list_at(key, &:pop) } end |
#rpoplpush(source, destination) ⇒ Object
218 219 220 221 222 |
# File 'lib/mock_redis/list_methods.rb', line 218 def rpoplpush(source, destination) value = rpop(source) lpush(destination, value) unless value.nil? value end |
#rpush(key, values) ⇒ Object
224 225 226 227 228 229 |
# File 'lib/mock_redis/list_methods.rb', line 224 def rpush(key, values) values = [values] unless values.is_a?(Array) assert_has_args(values, 'rpush') with_list_at(key) { |l| values.each { |v| l.push(v.to_s) } } llen(key) end |
#rpushx(key, value) ⇒ Object
231 232 233 234 235 236 237 238 239 |
# File 'lib/mock_redis/list_methods.rb', line 231 def rpushx(key, value) value = [value] unless value.is_a?(Array) if value.empty? raise Error.command_error("ERR wrong number of arguments for 'rpushx' command", self) end assert_listy(key) return 0 unless list_at?(key) rpush(key, value) end |