Skip to content
Merged
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/kredis/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 13 additions & 2 deletions test/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,6 +36,11 @@ def self.name
def id
8
end

private
def generate_key
"some-generated-key"
end
end

class MissingIdPerson
Expand Down Expand Up @@ -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")
Expand Down