Sha256: 0ad8e59059673e7cbbf20ff7a260c38d2392a3f8b560efabdac1aa2fd8985b33

Contents?: true

Size: 1.34 KB

Versions: 8

Compression:

Stored size: 1.34 KB

Contents

class MockRedis
  class FutureNotReady < RuntimeError; end

  class Future
    attr_reader :command

    def initialize(command)
      @command = command
      @result_set = false
    end

    def value
      raise FutureNotReady unless @result_set
      @result
    end

    def set_result(result)
      @result_set = true
      @result = result
    end
  end

  class PipelinedWrapper
    include UndefRedisMethods

    def respond_to?(method, include_private=false)
      super || @db.respond_to?(method)
    end

    def initialize(db)
      @db = db
      @pipelined_futures = []
      @in_pipeline = false
    end

    def initialize_copy(source)
      super
      @db = @db.clone
      @pipelined_futures = @pipelined_futures.clone
    end

    def method_missing(method, *args, &block)
      if @in_pipeline
        future = MockRedis::Future.new([method, *args])
        @pipelined_futures << future
        future
      else
        @db.send(method, *args, &block)
      end
    end

    def pipelined(options = {})
      @in_pipeline = true
      yield self
      @in_pipeline = false
      responses = @pipelined_futures.map do |future|
        begin
          result = send(*future.command)
          future.set_result(result)
          result
        rescue => e
          e
        end
      end
      @pipelined_futures = []
      responses
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mock_redis-0.13.2 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.13.1 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.13.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.12.1 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.12.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.11.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.10.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.9.0 lib/mock_redis/pipelined_wrapper.rb