Skip to content

Commit 8a5a55f

Browse files
authored
Bring expires_in configuration to Kredis::Attributes (#43)
Expiring scalars came to Kredis in #19. The expiration configuration was available when working directly with types, but was not made available when defining Kredis values on an `ActiveRecord`/`ActiveModel` class. This change makes the `expires_in` option available when working with scalars on `ActiveRecord/ActiveModel`.
1 parent bbcae39 commit 8a5a55f

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

lib/kredis/attributes.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ def kredis_proxy(name, key: nil, config: :shared, after_change: nil)
66
kredis_connection_with __method__, name, key, config: config, after_change: after_change
77
end
88

9-
def kredis_string(name, key: nil, config: :shared, after_change: nil)
10-
kredis_connection_with __method__, name, key, config: config, after_change: after_change
9+
def kredis_string(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
10+
kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
1111
end
1212

13-
def kredis_integer(name, key: nil, config: :shared, after_change: nil)
14-
kredis_connection_with __method__, name, key, config: config, after_change: after_change
13+
def kredis_integer(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
14+
kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
1515
end
1616

17-
def kredis_decimal(name, key: nil, config: :shared, after_change: nil)
18-
kredis_connection_with __method__, name, key, config: config, after_change: after_change
17+
def kredis_decimal(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
18+
kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
1919
end
2020

21-
def kredis_datetime(name, key: nil, config: :shared, after_change: nil)
22-
kredis_connection_with __method__, name, key, config: config, after_change: after_change
21+
def kredis_datetime(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
22+
kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
2323
end
2424

2525
def kredis_flag(name, key: nil, config: :shared, after_change: nil)
@@ -30,16 +30,16 @@ def kredis_flag(name, key: nil, config: :shared, after_change: nil)
3030
end
3131
end
3232

33-
def kredis_float(name, key: nil, config: :shared, after_change: nil)
34-
kredis_connection_with __method__, name, key, config: config, after_change: after_change
33+
def kredis_float(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
34+
kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
3535
end
3636

3737
def kredis_enum(name, key: nil, values:, default:, config: :shared, after_change: nil)
3838
kredis_connection_with __method__, name, key, values: values, default: default, config: config, after_change: after_change
3939
end
4040

41-
def kredis_json(name, key: nil, config: :shared, after_change: nil)
42-
kredis_connection_with __method__, name, key, config: config, after_change: after_change
41+
def kredis_json(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
42+
kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
4343
end
4444

4545
def kredis_list(name, key: nil, typed: :string, config: :shared, after_change: nil)

test/attributes_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "test_helper"
2+
require "active_support/core_ext/integer"
23

34
class Person
45
include Kredis::Attributes
@@ -21,6 +22,7 @@ class Person
2122
kredis_set :vacations
2223
kredis_json :settings
2324
kredis_counter :amount
25+
kredis_string :temporary_password, expires_in: 1.second
2426
kredis_hash :high_scores, typed: :integer
2527

2628
def self.name
@@ -227,4 +229,11 @@ def suddenly_implemented_person.id; 8; end
227229

228230
assert_nil suddenly_implemented_person.anything.get
229231
end
232+
233+
test "expiring scalars" do
234+
@person.temporary_password.value = "assigned"
235+
assert_changes "@person.temporary_password.value", from: "assigned", to: nil do
236+
sleep 1.1.seconds
237+
end
238+
end
230239
end

0 commit comments

Comments
 (0)