Skip to content

Commit e217f77

Browse files
author
Mikhail Varabyou
committed
add List#size and List#first.
allow to pass range to List#elements. fix List#last to return typed.
1 parent 57cedf5 commit e217f77

File tree

2 files changed

+78
-12
lines changed

2 files changed

+78
-12
lines changed

lib/kredis/types/list.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
class Kredis::Types::List < Kredis::Types::Proxying
44
prepend Kredis::DefaultValues
55

6-
proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del
6+
proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del, :llen
77

88
attr_accessor :typed
99

10-
def elements
11-
strings_to_types(lrange(0, -1) || [], typed)
10+
def elements(start = 0, stop = -1)
11+
strings_to_types(lrange(start, stop) || [], typed)
1212
end
1313
alias to_a elements
1414

@@ -29,10 +29,20 @@ def clear
2929
del
3030
end
3131

32+
def first(n = nil)
33+
n ? elements(0, n - 1) : elements(0, 0).first
34+
end
35+
3236
def last(n = nil)
33-
n ? lrange(-n, -1) : lrange(-1, -1).first
37+
n ? elements(-n, -1) : elements(-1, -1).first
3438
end
3539

40+
def size
41+
llen
42+
end
43+
44+
alias length size
45+
3646
private
3747
def set_default
3848
append default

test/types/list_test.rb

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
class ListTest < ActiveSupport::TestCase
77
setup { @list = Kredis.list "mylist" }
88

9+
test "elements" do
10+
@list.clear
11+
@list.append %w[ 1 2 3 ]
12+
assert_equal %w[ 1 2 3 ], @list.elements
13+
end
14+
915
test "append" do
1016
@list.append(%w[ 1 2 3 ])
1117
@list << 4
@@ -43,6 +49,16 @@ class ListTest < ActiveSupport::TestCase
4349
assert_equal [], @list.elements
4450
end
4551

52+
test "first" do
53+
@list.prepend(%w[ 1 2 3 ])
54+
assert_equal "3", @list.first
55+
end
56+
57+
test "first(n)" do
58+
@list.prepend(%w[ 1 2 3 ])
59+
assert_equal %w[ 3 2 ], @list.first(2)
60+
end
61+
4662
test "last" do
4763
@list.append(%w[ 1 2 3 ])
4864
assert_equal "3", @list.last
@@ -53,14 +69,15 @@ class ListTest < ActiveSupport::TestCase
5369
assert_equal %w[ 2 3 ], @list.last(2)
5470
end
5571

56-
test "typed as datetime" do
57-
@list = Kredis.list "mylist", typed: :datetime
58-
59-
@list.append [ 1.day.from_now.midnight.in_time_zone("Pacific Time (US & Canada)"), 2.days.from_now.midnight.in_time_zone("UTC") ]
60-
assert_equal [ 1.day.from_now.midnight, 2.days.from_now.midnight ], @list.elements
72+
test "size" do
73+
@list.clear
74+
@list.append(%w[ 1 2 3 ])
75+
assert_equal 3, @list.size
76+
end
6177

62-
@list.remove(2.days.from_now.midnight)
63-
assert_equal [ 1.day.from_now.midnight ], @list.elements
78+
test "size when list removed" do
79+
@list.clear
80+
assert_equal 0, @list.size
6481
end
6582

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

79-
8096
test "default" do
8197
@list = Kredis.list "mylist", default: %w[ 1 2 3 ]
8298

@@ -132,3 +148,43 @@ class ListTest < ActiveSupport::TestCase
132148
assert_equal [ 0, 1, 2, 3, 4, 10, 20, 30 ], Kredis.list("mylist", typed: :integer).to_a.sort
133149
end
134150
end
151+
152+
class TypedListTest < ActiveSupport::TestCase
153+
setup { @list = Kredis.list "mylist.typed", typed: :integer }
154+
155+
test "elements" do
156+
@list.clear
157+
@list.append 1, 2, 3
158+
assert_equal [ 1, 2, 3 ], @list.elements
159+
end
160+
161+
test "first" do
162+
@list.prepend 1, 2, 3
163+
assert_equal 3, @list.first
164+
end
165+
166+
test "first(n)" do
167+
@list.prepend 3, 2, 1
168+
assert_equal [ 1, 2 ], @list.first(2)
169+
end
170+
171+
test "last" do
172+
@list.append 1, 2, 3
173+
assert_equal 3, @list.last
174+
end
175+
176+
test "last(n)" do
177+
@list.append 1, 2, 3
178+
assert_equal [ 2, 3 ], @list.last(2)
179+
end
180+
181+
test "typed as datetime" do
182+
@list = Kredis.list "mylist", typed: :datetime
183+
184+
@list.append [ 1.day.from_now.midnight.in_time_zone("Pacific Time (US & Canada)"), 2.days.from_now.midnight.in_time_zone("UTC") ]
185+
assert_equal [ 1.day.from_now.midnight, 2.days.from_now.midnight ], @list.elements
186+
187+
@list.remove(2.days.from_now.midnight)
188+
assert_equal [ 1.day.from_now.midnight ], @list.elements
189+
end
190+
end

0 commit comments

Comments
 (0)