diff --git a/README.md b/README.md index 66fcbf7..a4af3e5 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ integer_list = Kredis.list "myintegerlist", typed: :integer, default: [ 1, 2, 3 integer_list.append([ 4, 5, 6 ]) # => RPUSH myintegerlist "4" "5" "6" integer_list << 7 # => RPUSH myintegerlist "7" [ 1, 2, 3, 4, 5, 6, 7 ] == integer_list.elements # => LRANGE myintegerlist 0 -1 +integer_list.include? 7 # => LPOS myintegerlist 7, Requires Redis 6+ unique_list = Kredis.unique_list "myuniquelist" unique_list.append(%w[ 2 3 4 ]) # => LREM myuniquelist 0, "2" + LREM myuniquelist 0, "3" + LREM myuniquelist 0, "4" + RPUSH myuniquelist "2", "3", "4" diff --git a/lib/kredis/types/list.rb b/lib/kredis/types/list.rb index 0c1e06f..500031b 100644 --- a/lib/kredis/types/list.rb +++ b/lib/kredis/types/list.rb @@ -3,7 +3,7 @@ class Kredis::Types::List < Kredis::Types::Proxying prepend Kredis::DefaultValues - proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del + proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del, :lpos attr_accessor :typed @@ -33,6 +33,11 @@ def last(n = nil) n ? lrange(-n, -1) : lrange(-1, -1).first end + # Require Redis 6+ for LPOS + def include?(element) + !lpos(element).nil? + end + private def set_default append default diff --git a/test/types/list_test.rb b/test/types/list_test.rb index 7507548..75d6cfb 100644 --- a/test/types/list_test.rb +++ b/test/types/list_test.rb @@ -131,4 +131,12 @@ class ListTest < ActiveSupport::TestCase assert_equal [ 0, 1, 2, 3, 4, 10, 20, 30 ], Kredis.list("mylist", typed: :integer).to_a.sort end + + test "include?" do + list = Kredis.list "int-list", typed: :integer + list.append [ 1, 2, 3 ] + + assert list.include?(1) + assert_not list.include?(4) + end end diff --git a/test/types/unique_list_test.rb b/test/types/unique_list_test.rb index 8ac91ed..60226f8 100644 --- a/test/types/unique_list_test.rb +++ b/test/types/unique_list_test.rb @@ -93,4 +93,10 @@ class UniqueListTest < ActiveSupport::TestCase @list.prepend(%w[ 6 7 8 ]) assert_equal %w[ 8 7 6 1 2 3 ], @list.elements end + + test "include?" do + @list = Kredis.unique_list "myuniquelist", default: %w[ 1 2 3 ] + assert @list.include?("1") + assert_not @list.include?("4") + end end