Sha256: 52cefb7e17c58cb47a629900435b01b78e185cc6c83e4dbbb82da1486b5ea9aa

Contents?: true

Size: 1.41 KB

Versions: 8

Compression:

Stored size: 1.41 KB

Contents

require 'mock_redis/undef_redis_methods'

class MockRedis
  class TransactionWrapper
    include UndefRedisMethods

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

    def initialize(db)
      @db = db
      @queued_commands = []
      @in_multi = false
    end

    def method_missing(method, *args)
      if @in_multi
        @queued_commands << [method, *args]
        'QUEUED'
      else
        @db.expire_keys
        @db.send(method, *args)
      end
    end

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

    def discard
      unless @in_multi
        raise RuntimeError, "ERR DISCARD without MULTI"
      end
      @in_multi = false
      @queued_commands = []
      'OK'
    end

    def exec
      unless @in_multi
        raise RuntimeError, "ERR EXEC without MULTI"
      end
      @in_multi = false
      responses = @queued_commands.map do |cmd|
        begin
          send(*cmd)
        rescue => e
          e
        end
      end
      @queued_commands = []
      responses
    end

    def multi
      if @in_multi
        raise RuntimeError, "ERR MULTI calls can not be nested"
      end
      @in_multi = true
      if block_given?
        yield(self)
        self.exec
      else
        'OK'
      end
    end

    def unwatch
      'OK'
    end

    def watch(_)
      'OK'
    end

  end
end

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
redis_migrator-0.0.1 spec/mock_redis/lib/mock_redis/transaction_wrapper.rb
mock_redis-0.4.1 lib/mock_redis/transaction_wrapper.rb
mock_redis-0.4.0 lib/mock_redis/transaction_wrapper.rb
mock_redis-0.3.0 lib/mock_redis/transaction_wrapper.rb
ryansch-mock_redis-0.3.0 lib/mock_redis/transaction_wrapper.rb
ryansch-mock_redis-0.2.0.2 lib/mock_redis/transaction_wrapper.rb
ryansch-mock_redis-0.2.0.1 lib/mock_redis/transaction_wrapper.rb
mock_redis-0.2.0 lib/mock_redis/transaction_wrapper.rb