Sha256: e9061c861b5aa8be3fab207dc4237ed0c8bdf8e60b7574b7f682fc8957c33394

Contents?: true

Size: 1.37 KB

Versions: 12

Compression:

Stored size: 1.37 KB

Contents

class MockRedis
  class PipelinedWrapper
    include UndefRedisMethods

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

    def initialize(db)
      @db = db
      @pipelined_futures = []
      @nesting_level = 0
    end

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

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

    def pipelined(_options = {})
      begin
        @nesting_level += 1
        yield self
      ensure
        @nesting_level -= 1
      end

      if in_pipeline?
        return
      end

      responses = @pipelined_futures.flat_map do |future|
        result = if future.block
                   send(*future.command, &future.block)
                 else
                   send(*future.command)
                 end
        future.store_result(result)

        if future.block
          result
        else
          [result]
        end
      rescue StandardError => e
        e
      end
      @pipelined_futures = []
      responses
    end

    private

    def in_pipeline?
      @nesting_level > 0
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
mock_redis-0.48.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.47.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.46.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.45.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.44.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.43.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.42.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.41.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.40.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.39.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.38.0 lib/mock_redis/pipelined_wrapper.rb
mock_redis-0.37.0 lib/mock_redis/pipelined_wrapper.rb