module MockRedis::UtilityMethods

Private Instance Methods

clean_up_empties_at(key) click to toggle source
# File lib/mock_redis/utility_methods.rb, line 20
def clean_up_empties_at(key)
  if data[key]&.empty? && data[key] != '' && !data[key].is_a?(Stream)
    del(key)
  end
end
common_scan(values, cursor, opts = {}) click to toggle source
# File lib/mock_redis/utility_methods.rb, line 26
def common_scan(values, cursor, opts = {})
  count = (opts[:count] || 10).to_i
  cursor = cursor.to_i
  match = opts[:match] || '*'
  key = opts[:key] || lambda { |x| x }
  type_opt = opts[:type]
  filtered_values = []

  limit = cursor + count
  next_cursor = limit >= values.length ? '0' : limit.to_s

  unless values[cursor...limit].nil?
    filtered_values = values[cursor...limit].select do |val|
      redis_pattern_to_ruby_regex(match).match(key.call(val)) &&
        (type_opt.nil? || type(val) == type_opt)
    end
  end

  [next_cursor, filtered_values]
end
left_pad(str, size) click to toggle source
# File lib/mock_redis/utility_methods.rb, line 72
def left_pad(str, size)
  str = "0#{str}" while str.length < size

  str
end
primitive?(value) click to toggle source
# File lib/mock_redis/utility_methods.rb, line 16
def primitive?(value)
  value.is_a?(::Array) || value.is_a?(::Hash) || value.is_a?(::String)
end
twos_complement_decode(array) click to toggle source
# File lib/mock_redis/utility_methods.rb, line 61
def twos_complement_decode(array)
  total = 0

  array.each.with_index do |bit, index|
    total += 2**(array.length - index - 1) if bit == 1
    total = -total if index == 0
  end

  total
end
twos_complement_encode(n, size) click to toggle source
# File lib/mock_redis/utility_methods.rb, line 47
def twos_complement_encode(n, size)
  if n < 0
    str = (n + 1).abs.to_s(2)

    binary = left_pad(str, size - 1).chars.map { |c| c == '0' ? 1 : 0 }
    binary.unshift(1)
  else
    binary = left_pad(n.abs.to_s(2), size - 1).chars.map(&:to_i)
    binary.unshift(0)
  end

  binary
end
with_thing_at(key, assertion, empty_thing_generator) { |data| ... } click to toggle source
# File lib/mock_redis/utility_methods.rb, line 5
def with_thing_at(key, assertion, empty_thing_generator)
  send(assertion, key)
  data[key] ||= empty_thing_generator.call
  data_key_ref = data[key]
  ret = yield data[key]
  data[key] = data_key_ref if data[key].nil?
  primitive?(ret) ? ret.dup : ret
ensure
  clean_up_empties_at(key)
end