Skip to content

Commit

Permalink
add List#size and List#first.
Browse files Browse the repository at this point in the history
allow to pass range to List#elements.
fix List#last to return typed.
  • Loading branch information
Mikhail Varabyou committed Mar 31, 2024
1 parent 57cedf5 commit e217f77
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 12 deletions.
18 changes: 14 additions & 4 deletions lib/kredis/types/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
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, :llen

attr_accessor :typed

def elements
strings_to_types(lrange(0, -1) || [], typed)
def elements(start = 0, stop = -1)
strings_to_types(lrange(start, stop) || [], typed)
end
alias to_a elements

Expand All @@ -29,10 +29,20 @@ def clear
del
end

def first(n = nil)
n ? elements(0, n - 1) : elements(0, 0).first
end

def last(n = nil)
n ? lrange(-n, -1) : lrange(-1, -1).first
n ? elements(-n, -1) : elements(-1, -1).first
end

def size
llen
end

alias length size

private
def set_default
append default
Expand Down
72 changes: 64 additions & 8 deletions test/types/list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
class ListTest < ActiveSupport::TestCase
setup { @list = Kredis.list "mylist" }

test "elements" do
@list.clear
@list.append %w[ 1 2 3 ]
assert_equal %w[ 1 2 3 ], @list.elements
end

test "append" do
@list.append(%w[ 1 2 3 ])
@list << 4
Expand Down Expand Up @@ -43,6 +49,16 @@ class ListTest < ActiveSupport::TestCase
assert_equal [], @list.elements
end

test "first" do
@list.prepend(%w[ 1 2 3 ])
assert_equal "3", @list.first
end

test "first(n)" do
@list.prepend(%w[ 1 2 3 ])
assert_equal %w[ 3 2 ], @list.first(2)
end

test "last" do
@list.append(%w[ 1 2 3 ])
assert_equal "3", @list.last
Expand All @@ -53,14 +69,15 @@ class ListTest < ActiveSupport::TestCase
assert_equal %w[ 2 3 ], @list.last(2)
end

test "typed as datetime" do
@list = Kredis.list "mylist", typed: :datetime

@list.append [ 1.day.from_now.midnight.in_time_zone("Pacific Time (US & Canada)"), 2.days.from_now.midnight.in_time_zone("UTC") ]
assert_equal [ 1.day.from_now.midnight, 2.days.from_now.midnight ], @list.elements
test "size" do
@list.clear
@list.append(%w[ 1 2 3 ])
assert_equal 3, @list.size
end

@list.remove(2.days.from_now.midnight)
assert_equal [ 1.day.from_now.midnight ], @list.elements
test "size when list removed" do
@list.clear
assert_equal 0, @list.size
end

test "exists?" do
Expand All @@ -76,7 +93,6 @@ class ListTest < ActiveSupport::TestCase
assert_equal %w[ 2 3 ], @list.elements
end


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

Expand Down Expand Up @@ -132,3 +148,43 @@ class ListTest < ActiveSupport::TestCase
assert_equal [ 0, 1, 2, 3, 4, 10, 20, 30 ], Kredis.list("mylist", typed: :integer).to_a.sort
end
end

class TypedListTest < ActiveSupport::TestCase
setup { @list = Kredis.list "mylist.typed", typed: :integer }

test "elements" do
@list.clear
@list.append 1, 2, 3
assert_equal [ 1, 2, 3 ], @list.elements
end

test "first" do
@list.prepend 1, 2, 3
assert_equal 3, @list.first
end

test "first(n)" do
@list.prepend 3, 2, 1
assert_equal [ 1, 2 ], @list.first(2)
end

test "last" do
@list.append 1, 2, 3
assert_equal 3, @list.last
end

test "last(n)" do
@list.append 1, 2, 3
assert_equal [ 2, 3 ], @list.last(2)
end

test "typed as datetime" do
@list = Kredis.list "mylist", typed: :datetime

@list.append [ 1.day.from_now.midnight.in_time_zone("Pacific Time (US & Canada)"), 2.days.from_now.midnight.in_time_zone("UTC") ]
assert_equal [ 1.day.from_now.midnight, 2.days.from_now.midnight ], @list.elements

@list.remove(2.days.from_now.midnight)
assert_equal [ 1.day.from_now.midnight ], @list.elements
end
end

0 comments on commit e217f77

Please sign in to comment.