Skip to content

Commit 8e93b7c

Browse files
authored
Merge pull request #920 from simonrussell/silence-exists-message
Add ability to silence `exists` warning message.
2 parents 8982fa2 + 4d72b11 commit 8e93b7c

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
* Setting `Redis.exists_returns_integer = false` disables warning message about new behaviour.
4+
35
# 4.2.0
46

57
* Convert commands to accept keyword arguments rather than option hashes. This both help catching typos, and reduce needless allocations.

lib/redis.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,20 @@
55

66
class Redis
77
class << self
8-
attr_accessor :exists_returns_integer
8+
attr_reader :exists_returns_integer
9+
10+
def exists_returns_integer=(value)
11+
unless value
12+
message = "`Redis#exists(key)` will return an Integer by default in redis-rb 4.3. The option to explicitly " \
13+
"disable this behaviour via `Redis.exists_returns_integer` will be removed in 5.0. You should use " \
14+
"`exists?` instead. "
15+
16+
::Kernel.warn(message)
17+
end
18+
19+
@exists_returns_integer = value
20+
end
21+
922
attr_writer :current
1023
end
1124

@@ -561,11 +574,16 @@ def unlink(*keys)
561574
# @return [Integer]
562575
def exists(*keys)
563576
if !Redis.exists_returns_integer && keys.size == 1
564-
message = "`Redis#exists(key)` will return an Integer in redis-rb 4.3, if you want to keep the old behavior, " \
565-
"use `exists?` instead. To opt-in to the new behavior now you can set Redis.exists_returns_integer = true. " \
566-
"(#{::Kernel.caller(1, 1).first})\n"
577+
if Redis.exists_returns_integer.nil?
578+
message = "`Redis#exists(key)` will return an Integer in redis-rb 4.3. `exists?` returns a boolean, you " \
579+
"should use it instead. To opt-in to the new behavior now you can set Redis.exists_returns_integer = " \
580+
"true. To disable this message and keep the current (boolean) behaviour of 'exists' you can set " \
581+
"`Redis.exists_returns_integer = false`, but this option will be removed in 5.0. " \
582+
"(#{::Kernel.caller(1, 1).first})\n"
583+
584+
::Kernel.warn(message)
585+
end
567586

568-
::Kernel.warn(message)
569587
exists?(*keys)
570588
else
571589
_exists(*keys)

0 commit comments

Comments
 (0)