Sha256: f0dc1edac6334fedd72d088a452fe8154b2878a91eaba4699446f4eca9c54e6b

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

require 'redis'
require 'hella-redis/connection'

module HellaRedis

  class ConnectionSpy

    attr_reader :config, :redis_spy, :with_calls

    def initialize(config, redis_spy = nil)
      @config     = config
      @with_calls = []
      @redis_spy  = redis_spy || RedisSpy.new(config)
    end

    def with(*args, &block)
      @with_calls << WithCall.new(args, block)
      block.call(@redis_spy)
    end

    def redis_calls
      @redis_spy.calls
    end

  end

  class RedisSpy

    attr_reader :calls

    def initialize(config)
      # mimic the real conection behavior, accept hash or object
      config = Connection::Config.new(config) if config.kind_of?(::Hash)
      @instance = ::Redis.new({
        :url    => config.url,
        :driver => config.driver
      })
      @calls = []
    end

    def method_missing(name, *args, &block)
      if self.respond_to?(name)
        @calls << RedisCall.new(name, args, block)
      else
        super
      end
    end

    def respond_to?(*args)
      super || @instance.respond_to?(*args)
    end

  end

  WithCall  = Struct.new(:args, :block)
  RedisCall = Struct.new(:command, :args, :block)

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hella-redis-0.2.1 lib/hella-redis/connection_spy.rb
hella-redis-0.2.0 lib/hella-redis/connection_spy.rb