Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add include? to list, uniquelist (re-open) #150

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 6 additions & 1 deletion lib/kredis/types/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/types/list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions test/types/unique_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading