Skip to content

Add support for list default value #73

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions lib/kredis/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def kredis_json(name, key: nil, config: :shared, after_change: nil, expires_in:
kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
end

def kredis_list(name, key: nil, typed: :string, config: :shared, after_change: nil)
kredis_connection_with __method__, name, key, typed: typed, config: config, after_change: after_change
def kredis_list(name, key: nil, typed: :string, config: :shared, after_change: nil, default: nil)
kredis_connection_with __method__, name, key, typed: typed, config: config, after_change: after_change, default: default
end

def kredis_unique_list(name, limit: nil, key: nil, typed: :string, config: :shared, after_change: nil)
Expand Down
4 changes: 2 additions & 2 deletions lib/kredis/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def hash(key, typed: :string, config: :shared, after_change: nil)
type_from(Hash, config, key, after_change: after_change, typed: typed)
end

def list(key, typed: :string, config: :shared, after_change: nil)
type_from(List, config, key, after_change: after_change, typed: typed)
def list(key, typed: :string, config: :shared, after_change: nil, default: nil)
type_from(List, config, key, after_change: after_change, typed: typed, default: default)
end

def unique_list(key, typed: :string, limit: nil, config: :shared, after_change: nil)
Expand Down
17 changes: 15 additions & 2 deletions lib/kredis/types/list.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
class Kredis::Types::List < Kredis::Types::Proxying
proxying :lrange, :lrem, :lpush, :rpush, :exists?, :del

attr_accessor :typed
attr_accessor :typed, :default

def elements
strings_to_types(lrange(0, -1) || [], typed)
raw_value = lrange(0, -1)

if raw_value.empty?
raw_value = list_default_evaluated
append(raw_value)
end

strings_to_types(raw_value, typed)
end
alias to_a elements

Expand All @@ -24,4 +31,10 @@ def append(*elements)
def clear
del
end

private

def list_default_evaluated
default.is_a?(Proc) ? default.call(self) : default
end
end
8 changes: 6 additions & 2 deletions test/types/list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
require "active_support/core_ext/integer"

class ListTest < ActiveSupport::TestCase
setup { @list = Kredis.list "mylist" }
setup { @list = Kredis.list "mylist", default: ->(_) { %w[ 1 2 3 ] } }

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

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

test "typed as datetime" do
Expand Down