Sha256: ed029be516fdc22f7733abcfa8806a7d6956a8996f2d329cf4ef73f5dcd0815a

Contents?: true

Size: 1.62 KB

Versions: 16

Compression:

Stored size: 1.62 KB

Contents

module BetterRecord
  class FakeRedis
    def data
      @data ||= {}
    end

    def incr(key)
      data[key] ||= 0
      data[key] += 1
    end

    def decr(key)
      data[key] ||= 0
      data[key] -= 1
    end

    def set(key, val)
      data[key] = val
    end

    def get(key)
      data[key].dup
    end

    def lpush(key, val)
      require_array
      data[key].unshift val
      data[key].size
    end

    def lrem(key, count, val)
      require_array
      start_idx = 0
      arr = data[key]
      removed = 0

      if count == 0
        removed = (arr.select {|v| v != val}).size
        arr.select! {|v| v != val}
      else
        arr.reverse! if count < 0

        count.abs.times do
          removed += 1 unless arr.delete_at(arr.index(val) || arr.size).nil?
        end

        arr.reverse! if count < 0
      end

      data[key] = arr
      removed
    end

    def lrange(key, start_idx, end_idx = -1)
      data[key][start_idx..end_idx] || []
    end

    def rpush(key, val)
      require_array
      data[key] << val
      data[key].size
    end

    def rpushx(key, val)
      return 0 unless data[key]
      rpush key, val
    end

    def rpop(key)
      require_array
      data[key].pop
    end

    def rpoplpush(one, two)
      val = rpop(one)
      lpush two, val
      val
    end

    def method_missing
      return nil
    end

    private
      def require_array(key)
        data[key] ||= []
        raise_wrong_type unless data[key].is_a?(Array)
      end

      def raise_wrong_type
        raise CommandError.new("WRONGTYPE Operation against a key holding the wrong kind of value")
      end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
better_record-0.8.0 lib/better_record/fake_redis.rb
better_record-0.7.5 lib/better_record/fake_redis.rb
better_record-0.7.4 lib/better_record/fake_redis.rb
better_record-0.7.3 lib/better_record/fake_redis.rb
better_record-0.7.2 lib/better_record/fake_redis.rb
better_record-0.7.1 lib/better_record/fake_redis.rb
better_record-0.7.0 lib/better_record/fake_redis.rb
better_record-0.6.1 lib/better_record/fake_redis.rb
better_record-0.6.0 lib/better_record/fake_redis.rb
better_record-0.5.5 lib/better_record/fake_redis.rb
better_record-0.5.4 lib/better_record/fake_redis.rb
better_record-0.5.1 lib/better_record/fake_redis.rb
better_record-0.5.0 lib/better_record/fake_redis.rb
better_record-0.4.0 lib/better_record/fake_redis.rb
better_record-0.3.0 lib/better_record/fake_redis.rb
better_record-0.2.0 lib/better_record/fake_redis.rb