class MockRedis

TODO: Implement the following commands

* xgroup
* xreadgroup
* xack
* xpending
* xclaim
* xinfo
* xtrim
* xdel

TODO: Complete support for

* xtrim
    - `approximate: true` argument is currently ignored
* xadd
    - `approximate: true` argument (for capped streams) is currently ignored

For details of these commands see

* https://redis.io/topics/streams-intro
* https://redis.io/commands#stream

Defines the gem version.

Constants

DEFAULTS
VERSION
WouldBlock

Attributes

options[R]

Public Class Methods

connect(*args) click to toggle source
# File lib/mock_redis.rb, line 29
def self.connect(*args)
  new(*args)
end
new(*args) click to toggle source
# File lib/mock_redis.rb, line 33
def initialize(*args)
  @options = _parse_options(args.first)

  @db = PipelinedWrapper.new(
    TransactionWrapper.new(
      ExpireWrapper.new(
        MultiDbWrapper.new(
          Database.new(self, *args)
        )
      )
    )
  )
end

Public Instance Methods

client() click to toggle source
# File lib/mock_redis.rb, line 72
def client
  self
end
connect() click to toggle source
# File lib/mock_redis.rb, line 76
def connect
  self
end
db() click to toggle source
# File lib/mock_redis.rb, line 60
def db
  options[:db]
end
host() click to toggle source
# File lib/mock_redis.rb, line 52
def host
  options[:host]
end
id() click to toggle source
# File lib/mock_redis.rb, line 47
def id
  "redis://#{host}:#{port}/#{db}"
end
Also aliased as: location
initialize_copy(source) click to toggle source
Calls superclass method
# File lib/mock_redis.rb, line 98
def initialize_copy(source)
  super
  @db = @db.clone
end
location()
Alias for: id
logger() click to toggle source
# File lib/mock_redis.rb, line 64
def logger
  options[:logger]
end
method_missing(method, *args, &block) click to toggle source
# File lib/mock_redis.rb, line 92
               def method_missing(method, *args, &block)
  logging([[method, *args]]) do
    @db.send(method, *args, &block)
  end
end
port() click to toggle source
# File lib/mock_redis.rb, line 56
def port
  options[:port]
end
reconnect() click to toggle source
# File lib/mock_redis.rb, line 80
def reconnect
  self
end
respond_to?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/mock_redis.rb, line 88
def respond_to?(method, include_private = false)
  super || @db.respond_to?(method, include_private)
end
time_at(timestamp) click to toggle source
# File lib/mock_redis.rb, line 68
def time_at(timestamp)
  options[:time_class].at(timestamp)
end
with() { |self| ... } click to toggle source
# File lib/mock_redis.rb, line 84
def with
  yield self
end

Protected Instance Methods

_parse_options(options) click to toggle source
# File lib/mock_redis.rb, line 105
def _parse_options(options)
  return DEFAULTS.dup if options.nil?

  defaults = DEFAULTS.dup

  url = options[:url] || ENV['REDIS_URL']

  # Override defaults from URL if given
  if url
    require 'uri'

    uri = URI(url)

    if uri.scheme == 'unix'
      defaults[:path] = uri.path
    else
      # Require the URL to have at least a host
      raise ArgumentError, 'invalid url' unless uri.host

      defaults[:scheme]   = uri.scheme
      defaults[:host]     = uri.host
      defaults[:port]     = uri.port if uri.port
      defaults[:password] = uri.password if uri.password
      defaults[:db]       = uri.path[1..].to_i if uri.path
    end
  end

  options = defaults.merge(options)

  if options[:path]
    options[:scheme] = 'unix'
    options.delete(:host)
    options.delete(:port)
  else
    options[:host] = options[:host].to_s
    options[:port] = options[:port].to_i
  end

  options[:timeout] = options[:timeout].to_f
  options[:db] = options[:db].to_i

  options
end
logging(commands) { || ... } click to toggle source
# File lib/mock_redis.rb, line 149
def logging(commands)
  return yield unless logger&.debug?

  begin
    commands.each do |name, *args|
      logged_args = args.map do |a|
        if a.respond_to?(:inspect) then a.inspect
        elsif a.respond_to?(:to_s) then a.to_s
        else
          # handle poorly-behaved descendants of BasicObject
          klass = a.instance_exec { (class << self; self end).superclass }
          "\#<#{klass}:#{a.__id__}>"
        end
      end
      logger.debug("[MockRedis] command=#{name.to_s.upcase} args=#{logged_args.join(' ')}")
    end

    t1 = Time.now
    yield
  ensure
    if t1
      logger.debug(format('[MockRedis] call_time=%<time>0.2f ms', time: ((Time.now - t1) * 1000)))
    end
  end
end