Skip to content

Commit da08c12

Browse files
Provide Hash#clear and List#clear (#63)
* Provide Hash#clear and List#clear * Proxy :del for List
1 parent f925702 commit da08c12

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

lib/kredis/types/hash.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def delete(*keys)
2828
def remove
2929
del
3030
end
31+
alias clear remove
3132

3233
def entries
3334
(hgetall || {}).transform_values { |val| string_to_type(val, typed) }.with_indifferent_access

lib/kredis/types/list.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Kredis::Types::List < Kredis::Types::Proxying
2-
proxying :lrange, :lrem, :lpush, :rpush, :exists?
2+
proxying :lrange, :lrem, :lpush, :rpush, :exists?, :del
33

44
attr_accessor :typed
55

@@ -20,4 +20,8 @@ def append(*elements)
2020
rpush types_to_strings(elements, typed) if elements.flatten.any?
2121
end
2222
alias << append
23+
24+
def clear
25+
del
26+
end
2327
end

test/types/hash_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ class HashTest < ActiveSupport::TestCase
7777
assert_equal({}, @hash.to_h)
7878
end
7979

80+
test "clear" do
81+
@hash.update("key2" => "value2")
82+
assert_equal "value2", @hash["key2"]
83+
@hash.clear
84+
assert_equal({}, @hash.to_h)
85+
end
86+
8087
test "exists?" do
8188
assert_not @hash.exists?
8289

test/types/list_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class ListTest < ActiveSupport::TestCase
3535
assert_equal %w[ 4 ], @list.elements
3636
end
3737

38+
test "clear" do
39+
@list.append(%w[ 1 2 3 4 ])
40+
@list.clear
41+
assert_equal [], @list.elements
42+
end
43+
3844
test "typed as datetime" do
3945
@list = Kredis.list "mylist", typed: :datetime
4046

0 commit comments

Comments
 (0)