diff --git a/lib/kredis/attributes.rb b/lib/kredis/attributes.rb index 2bb1277..389ac6c 100644 --- a/lib/kredis/attributes.rb +++ b/lib/kredis/attributes.rb @@ -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) diff --git a/lib/kredis/types.rb b/lib/kredis/types.rb index 16ad906..c389316 100644 --- a/lib/kredis/types.rb +++ b/lib/kredis/types.rb @@ -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) diff --git a/lib/kredis/types/list.rb b/lib/kredis/types/list.rb index cbda3bd..dc543ae 100644 --- a/lib/kredis/types/list.rb +++ b/lib/kredis/types/list.rb @@ -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 @@ -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 diff --git a/test/types/list_test.rb b/test/types/list_test.rb index 7e7aa5f..e17830c 100644 --- a/test/types/list_test.rb +++ b/test/types/list_test.rb @@ -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 ]) @@ -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