Skip to content

Commit

Permalink
Add ability to silence exists warning message.
Browse files Browse the repository at this point in the history
The warning message can be disabled by setting `exists_returns_integer`
to `false`.  You will receive a single warning message when this is set.
  • Loading branch information
simonrussell committed Jun 11, 2020
1 parent 8982fa2 commit 4d72b11
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
# Unreleased

* Setting `Redis.exists_returns_integer = false` disables warning message about new behaviour.

# 4.2.0

* Convert commands to accept keyword arguments rather than option hashes. This both help catching typos, and reduce needless allocations.
Expand Down
28 changes: 23 additions & 5 deletions lib/redis.rb
Expand Up @@ -5,7 +5,20 @@

class Redis
class << self
attr_accessor :exists_returns_integer
attr_reader :exists_returns_integer

def exists_returns_integer=(value)
unless value
message = "`Redis#exists(key)` will return an Integer by default in redis-rb 4.3. The option to explicitly " \
"disable this behaviour via `Redis.exists_returns_integer` will be removed in 5.0. You should use " \
"`exists?` instead. "

::Kernel.warn(message)
end

@exists_returns_integer = value
end

attr_writer :current
end

Expand Down Expand Up @@ -561,11 +574,16 @@ def unlink(*keys)
# @return [Integer]
def exists(*keys)
if !Redis.exists_returns_integer && keys.size == 1
message = "`Redis#exists(key)` will return an Integer in redis-rb 4.3, if you want to keep the old behavior, " \
"use `exists?` instead. To opt-in to the new behavior now you can set Redis.exists_returns_integer = true. " \
"(#{::Kernel.caller(1, 1).first})\n"
if Redis.exists_returns_integer.nil?
message = "`Redis#exists(key)` will return an Integer in redis-rb 4.3. `exists?` returns a boolean, you " \
"should use it instead. To opt-in to the new behavior now you can set Redis.exists_returns_integer = " \
"true. To disable this message and keep the current (boolean) behaviour of 'exists' you can set " \
"`Redis.exists_returns_integer = false`, but this option will be removed in 5.0. " \
"(#{::Kernel.caller(1, 1).first})\n"

::Kernel.warn(message)
end

::Kernel.warn(message)
exists?(*keys)
else
_exists(*keys)
Expand Down

0 comments on commit 4d72b11

Please sign in to comment.