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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class Person < ApplicationRecord
kredis_list :names_with_custom_key, key: ->(p) { "person:#{p.id}:names_customized" }
kredis_unique_list :skills, limit: 2
kredis_enum :morning, values: %w[ bright blue black ], default: "bright"
kredis_counter :steps, expires_in: 1.hour
end

person = Person.find(5)
Expand Down
4 changes: 2 additions & 2 deletions lib/kredis/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def kredis_slots(name, available:, key: nil, config: :shared, after_change: nil)
kredis_connection_with __method__, name, key, available: available, config: config, after_change: after_change
end

def kredis_counter(name, key: nil, config: :shared, after_change: nil)
kredis_connection_with __method__, name, key, config: config, after_change: after_change
def kredis_counter(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
end

def kredis_hash(name, key: nil, typed: :string, config: :shared, after_change: nil)
Expand Down
8 changes: 8 additions & 0 deletions test/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Person
kredis_set :vacations
kredis_json :settings
kredis_counter :amount
kredis_counter :expiring_amount, expires_in: 1.second
kredis_string :temporary_password, expires_in: 1.second
kredis_hash :high_scores, typed: :integer

Expand Down Expand Up @@ -211,6 +212,13 @@ class AttributesTest < ActiveSupport::TestCase
assert_equal 0, @person.amount.value
end

test "counter with expires_at" do
@person.expiring_amount.increment
assert_changes "@person.expiring_amount.value", from: 1, to: 0 do
sleep 1.1.seconds
end
end

test "hash" do
@person.high_scores.update(space_invaders: 100, pong: 42)
assert_equal({ "space_invaders" => 100, "pong" => 42 }, @person.high_scores.to_h)
Expand Down