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 30
def self.connect(*args)
  new(*args)
end
new(*args) click to toggle source
# File lib/mock_redis.rb, line 34
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

call(command, &_block) click to toggle source
# File lib/mock_redis.rb, line 53
def call(command, &_block)
  send(*command)
end
client() click to toggle source
# File lib/mock_redis.rb, line 77
def client
  self
end
connect() click to toggle source
# File lib/mock_redis.rb, line 81
def connect
  self
end
db() click to toggle source
# File lib/mock_redis.rb, line 65
def db
  options[:db]
end
host() click to toggle source
# File lib/mock_redis.rb, line 57
def host
  options[:host]
end
id() click to toggle source
# File lib/mock_redis.rb, line 48
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 99
def initialize_copy(source)
  super
  @db = @db.clone
end
location()
Alias for: id
logger() click to toggle source
# File lib/mock_redis.rb, line 69
def logger
  options[:logger]
end
method_missing(method, *args, &block) click to toggle source
# File lib/mock_redis.rb, line 93
               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 61
def port
  options[:port]
end
reconnect() click to toggle source
# File lib/mock_redis.rb, line 85
def reconnect
  self
end
respond_to?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/mock_redis.rb, line 89
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 73
def time_at(timestamp)
  options[:time_class].at(timestamp)
end

Protected Instance Methods

_parse_options(options) click to toggle source
# File lib/mock_redis.rb, line 106
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..-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 150
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
    logger.debug("[MockRedis] call_time=%0.2f ms" % ((Time.now - t1) * 1000)) if t1
  end
end