# Simple mock Redis library. Only implements GET, INCR, MGET, KEYS, and
# EXPIRE. (EXPIRE is implemented as a noop, so don't rely on it.)

class MockRedis
  def initialize
    reset!
  end

  def reset!
    @data = {}
  end

  def incr(key)
    @data[key] ||= 0
    @data[key] += 1
  end

  def get(key)
    @data[key]
  end

  def keys(pattern)
    # pattern is a Redis-style pattern, not a Ruby regexp.
    re = regex_from_redis_pattern(pattern)
    @data.keys.find_all {|k| k =~ re}
  end

  def mget(*keys)
    keys.map {|k| @data[k]}
  end

  def expire(key, seconds)
    @data[key] ? 1 : 0
  end

  private

  def regex_from_redis_pattern(pattern)
    pattern.scan(%r/(\\.)|(.)/).map do |pair|
      if pair.first # this is backslash escape
        Regexp.escape(pair.first[1])
      else          # everything else
        case pair.last
        when '?'
          '.'
        when '*'
          '.*'
        else
          Regexp.escape(pair.last)
        end
      end
    end.join
  end
end

# Simple mock Redis library. Only implements GET, INCR, MGET, KEYS, and
# EXPIRE. (EXPIRE is implemented as a noop, so don't rely on it.)

class MockRedis
  def initialize
    reset!
  end

  def reset!
    @data = {}
  end

  def incr(key)
    @data[key] ||= 0
    @data[key] += 1
  end

  def get(key)
    @data[key]
  end

  def keys(pattern)
    # pattern is a Redis-style pattern, not a Ruby regexp.
    re = regex_from_redis_pattern(pattern)
    @data.keys.find_all {|k| k =~ re}
  end

  def mget(*keys)
    keys.map {|k| @data[k]}
  end

  def expire(key, seconds)
    @data[key] ? 1 : 0
  end

  private

  def regex_from_redis_pattern(pattern)
    regexp = pattern.scan(%r/(\\.)|(.)/).map do |pair|
      if pair.first # this is backslash escape
        Regexp.escape(pair.first[1])
      else          # everything else
        case pair.last
        when '?'
          '.'
        when '*'
          '.*'
        else
          Regexp.escape(pair.last)
        end
      end
    end.join
    Regexp.new(regexp)
  end
end

Return an actual regexp from regex_from_redis_pattern
Return an actual regexp from regex_from_redis_pattern
