Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion lib/kredis/types/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Kredis::Types::Hash < Kredis::Types::Proxying
prepend Kredis::DefaultValues

proxying :hget, :hset, :hmget, :hdel, :hgetall, :hkeys, :hvals, :del, :exists?
proxying :hget, :hset, :hmget, :hdel, :hgetall, :hkeys, :hvals, :del, :exists?, :expire, :expireat

attr_accessor :typed

Expand Down Expand Up @@ -47,6 +47,10 @@ def values
strings_to_types(hvals || [], typed)
end

def expire_at(datetime)
expireat datetime.to_i
end

private
def set_default
update(**default)
Expand Down
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than give slicing arguments to #elements, would introduce #slice and have #elements call it.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would assert_equal 0, @list.size here rather than having a separate test for size after removal.

@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