diff --git a/lib/mock_redis/set_methods.rb b/lib/mock_redis/set_methods.rb index bf355b6..138a3db 100644 --- a/lib/mock_redis/set_methods.rb +++ b/lib/mock_redis/set_methods.rb @@ -137,6 +137,11 @@ def srem(key, members) end end + def srem?(key, members) + res = srem(key, members) + res.is_a?(Numeric) ? res > 0 : res + end + def sscan(key, cursor, opts = {}) common_scan(smembers(key), cursor, opts) end diff --git a/spec/commands/srem_spec.rb b/spec/commands/srem_spec.rb index 452f704..2c68b97 100644 --- a/spec/commands/srem_spec.rb +++ b/spec/commands/srem_spec.rb @@ -41,5 +41,20 @@ expect(@redises.srem(@key, [1, 2])).to eq(2) end + context 'srem?' do + it 'returns true if member is in the set' do + expect(@redises.srem?(@key, 'bert')).to eq(true) + end + + it 'returns false if member is not in the set' do + expect(@redises.srem?(@key, 'cookiemonster')).to eq(false) + end + + it 'removes member from the set' do + @redises.srem?(@key, 'ernie') + expect(@redises.smembers(@key)).to eq(['bert']) + end + end + it_should_behave_like 'a set-only command' end