From e89b2d8e0ac8fa7fa78619bf4c3d66dfebe75138 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Wed, 13 Apr 2022 16:14:36 +0200 Subject: [PATCH 1/2] Support configuring custom keys via method invocation --- lib/kredis/attributes.rb | 1 + test/attributes_test.rb | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/kredis/attributes.rb b/lib/kredis/attributes.rb index 2bb1277..21c3ce6 100644 --- a/lib/kredis/attributes.rb +++ b/lib/kredis/attributes.rb @@ -97,6 +97,7 @@ def kredis_key_evaluated(key) case key when String then key when Proc then key.call(self) + when Symbol then send(key) end end diff --git a/test/attributes_test.rb b/test/attributes_test.rb index c3ab96a..5f4df6d 100644 --- a/test/attributes_test.rb +++ b/test/attributes_test.rb @@ -8,7 +8,8 @@ class Person kredis_proxy :nothing, key: "something:else" kredis_proxy :something, key: ->(p) { "person:#{p.id}:something" } kredis_list :names - kredis_list :names_with_custom_key, key: ->(p) { "person:#{p.id}:names_customized" } + kredis_list :names_with_custom_key_via_lambda, key: ->(p) { "person:#{p.id}:names_customized" } + kredis_list :names_with_custom_key_via_method, key: :generate_key kredis_unique_list :skills, limit: 2 kredis_flag :special kredis_flag :temporary_special, expires_in: 1.second @@ -35,6 +36,11 @@ def self.name def id 8 end + + private + def generate_key + "some-generated-key" + end end class MissingIdPerson @@ -68,10 +74,15 @@ class AttributesTest < ActiveSupport::TestCase end test "list with custom proc key" do - @person.names_with_custom_key.append(%w[ david kasper ]) + @person.names_with_custom_key_via_lambda.append(%w[ david kasper ]) assert_equal %w[ david kasper ], Kredis.redis.lrange("person:8:names_customized", 0, -1) end + test "list with custom method key" do + @person.names_with_custom_key_via_method.append(%w[ david kasper ]) + assert_equal %w[ david kasper ], Kredis.redis.lrange("some-generated-key", 0, -1) + end + test "unique list" do @person.skills.prepend(%w[ trolling photography ]) @person.skills.prepend("racing") From 7b90152d3175b1f4f336504ecd3b1971d03b2ca1 Mon Sep 17 00:00:00 2001 From: Jacopo Date: Fri, 15 Apr 2022 14:47:01 +0200 Subject: [PATCH 2/2] Add example in Readme.md --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6344ed1..67800b5 100644 --- a/README.md +++ b/README.md @@ -168,10 +168,16 @@ You can use all these structures in models: ```ruby class Person < ApplicationRecord kredis_list :names - kredis_list :names_with_custom_key, key: ->(p) { "person:#{p.id}:names_customized" } + kredis_list :names_with_custom_key_via_lambda, key: ->(p) { "person:#{p.id}:names_customized" } + kredis_list :names_with_custom_key_via_method, key: :generate_names_key kredis_unique_list :skills, limit: 2 kredis_enum :morning, values: %w[ bright blue black ], default: "bright" kredis_counter :steps, expires_in: 1.hour + + private + def generate_names_key + "key-generated-from-private-method" + end end person = Person.find(5)