Skip to content

Commit

Permalink
Add expires_in support to kredis_counter attribute (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbstriker38 authored Feb 19, 2022
1 parent 6c74e89 commit 75d34d9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
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
kredis_boolean :onboarded
Expand Down Expand Up @@ -212,6 +213,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

0 comments on commit 75d34d9

Please sign in to comment.