module MockRedis::ListMethods

Public Instance Methods

blmove(source, destination, wherefrom, whereto, options = {}) click to toggle source
# File lib/mock_redis/list_methods.rb, line 9
def blmove(source, destination, wherefrom, whereto, options = {})
  options = { :timeout => options } if options.is_a?(Integer)
  timeout = options.is_a?(Hash) && options[: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) click to toggle source
# 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) click to toggle source
# 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, options = {}) click to toggle source
# File lib/mock_redis/list_methods.rb, line 49
def brpoplpush(source, destination, options = {})
  options = { :timeout => options } if options.is_a?(Integer)
  timeout = options.is_a?(Hash) && options[: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) click to toggle source
# File lib/mock_redis/list_methods.rb, line 63
def lindex(key, index)
  with_list_at(key) { |l| l[index.to_i] }
end
linsert(key, position, pivot, value) click to toggle source
# File lib/mock_redis/list_methods.rb, line 67
def linsert(key, position, pivot, value)
  unless %w[before after].include?(position.to_s)
    raise Redis::CommandError, '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) click to toggle source
# File lib/mock_redis/list_methods.rb, line 91
def llen(key)
  with_list_at(key, &:length)
end
lmove(source, destination, wherefrom, whereto) click to toggle source
# File lib/mock_redis/list_methods.rb, line 95
def lmove(source, destination, wherefrom, whereto)
  assert_listy(source)
  assert_listy(destination)

  wherefrom = wherefrom.to_s.downcase
  whereto = whereto.to_s.downcase

  unless %w[left right].include?(wherefrom) && %w[left right].include?(whereto)
    raise Redis::CommandError, 'ERR syntax error'
  end

  value = wherefrom == 'left' ? lpop(source) : rpop(source)
  (whereto == 'left' ? lpush(destination, value) : rpush(destination, value)) unless value.nil?
  value
end
lpop(key, count = nil) click to toggle source
# File lib/mock_redis/list_methods.rb, line 111
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) click to toggle source
# File lib/mock_redis/list_methods.rb, line 120
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) click to toggle source
# File lib/mock_redis/list_methods.rb, line 127
def lpushx(key, value)
  value = [value] unless value.is_a?(Array)
  if value.empty?
    raise Redis::CommandError, "ERR wrong number of arguments for 'lpushx' command"
  end
  assert_listy(key)
  return 0 unless list_at?(key)
  lpush(key, value)
end
lrange(key, start, stop) click to toggle source
# File lib/mock_redis/list_methods.rb, line 137
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) click to toggle source
# File lib/mock_redis/list_methods.rb, line 142
def lrem(key, count, value)
  unless looks_like_integer?(count.to_s)
    raise Redis::CommandError, 'ERR value is not an integer or out of range'
  end
  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) click to toggle source
# File lib/mock_redis/list_methods.rb, line 166
def lset(key, index, value)
  assert_listy(key)

  unless list_at?(key)
    raise Redis::CommandError, 'ERR no such key'
  end

  index = index.to_i
  unless (0...llen(key)).cover?(index)
    raise Redis::CommandError, 'ERR index out of range'
  end

  data[key][index] = value.to_s
  'OK'
end
ltrim(key, start, stop) click to toggle source
# File lib/mock_redis/list_methods.rb, line 182
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) click to toggle source
# File lib/mock_redis/list_methods.rb, line 189
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) click to toggle source
# File lib/mock_redis/list_methods.rb, line 198
def rpoplpush(source, destination)
  value = rpop(source)
  lpush(destination, value) unless value.nil?
  value
end
rpush(key, values) click to toggle source
# File lib/mock_redis/list_methods.rb, line 204
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) click to toggle source
# File lib/mock_redis/list_methods.rb, line 211
def rpushx(key, value)
  value = [value] unless value.is_a?(Array)
  if value.empty?
    raise Redis::CommandError, "ERR wrong number of arguments for 'rpushx' command"
  end
  assert_listy(key)
  return 0 unless list_at?(key)
  rpush(key, value)
end

Private Instance Methods

assert_listy(key) click to toggle source
# File lib/mock_redis/list_methods.rb, line 235
def assert_listy(key)
  unless listy?(key)
    # Not the most helpful error, but it's what redis-rb barfs up
    raise Redis::CommandError,
          'WRONGTYPE Operation against a key holding the wrong kind of value'
  end
end
first_nonempty_list(keys) click to toggle source
# File lib/mock_redis/list_methods.rb, line 243
def first_nonempty_list(keys)
  keys.find { |k| llen(k) > 0 }
end
list_at?(key) click to toggle source
# File lib/mock_redis/list_methods.rb, line 223
def list_at?(key)
  data[key] && listy?(key)
end
listy?(key) click to toggle source
# File lib/mock_redis/list_methods.rb, line 231
def listy?(key)
  data[key].nil? || data[key].is_a?(Array)
end
with_list_at(key, &blk) click to toggle source
# File lib/mock_redis/list_methods.rb, line 227
def with_list_at(key, &blk)
  with_thing_at(key, :assert_listy, proc { [] }, &blk)
end