Skip to content

Commit e258c74

Browse files
CoderMigueldhh
andauthored
Add the smove method to Kredis::Types::Set (#160)
* Add 'smove' method to Kredis::Types::Set the Redis documentation currently supports the use of the `smove` method, this change make that method also available in kredis example: my_kredis_set.smove(another_kredis_set.key, value_to_move) use case: when using sets as queues for a multi step process it is helpful to move values between sets: queued -> issued issued -> completed issued -> failed & add back to queued for retying https://redis.io/docs/latest/commands/smove/ * Add smove test coverage * Update lib/kredis/types/set.rb Co-authored-by: CoderMiguel <[email protected]> * Update test/types/set_test.rb Co-authored-by: CoderMiguel <[email protected]> * linter feedback * correct Gemfile.lock change --------- Co-authored-by: David Heinemeier Hansson <[email protected]>
1 parent 8850def commit e258c74

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

Diff for: lib/kredis/types/set.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
class Kredis::Types::Set < Kredis::Types::Proxying
44
prepend Kredis::DefaultValues
55

6-
proxying :smembers, :sadd, :srem, :multi, :del, :sismember, :scard, :spop, :exists?, :srandmember
6+
proxying :smembers, :sadd, :srem, :multi, :del, :sismember, :scard, :spop, :exists?, :srandmember, :smove
77

88
attr_accessor :typed
9-
9+
def move(set, member)
10+
destination = set.respond_to?(:key) ? set.key : set
11+
smove(destination, member)
12+
end
1013
def members
1114
strings_to_types(smembers || [], typed).sort
1215
end

Diff for: test/types/set_test.rb

+29
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,35 @@ class SetTest < ActiveSupport::TestCase
101101
assert_equal [ 1.5, 2.7 ], @set.sample(2).sort
102102
end
103103

104+
test "smove" do
105+
@set.add(%w[ 1 2 ])
106+
another_set = Kredis.set "another_set"
107+
another_set.add(%w[ 3 ])
108+
109+
assert @set.smove(another_set.key, "2")
110+
assert_equal %w[ 1 ], @set.members
111+
assert_equal %w[ 2 3 ], another_set.members
112+
end
113+
114+
test "move with set" do
115+
@set.add(%w[ x y ])
116+
another_set = Kredis.set "another_set"
117+
another_set.add(%w[ z ])
118+
119+
assert @set.move(another_set, "y")
120+
assert_equal %w[ x ], @set.members
121+
assert_equal %w[ y z ], another_set.members
122+
end
123+
124+
test "move with key" do
125+
@set.add(%w[ a b ])
126+
another_set = Kredis.set "another_set"
127+
another_set.add(%w[ c ])
128+
129+
assert @set.move(another_set.key, "b")
130+
assert_equal %w[ a ], @set.members
131+
assert_equal %w[ b c ], another_set.members
132+
end
104133

105134
test "default" do
106135
@set = Kredis.set "mylist", default: %w[ 1 2 3 ]

0 commit comments

Comments
 (0)