flipper-redis 0.27.1 → 0.28.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/redis/internals.rb +2 -2
- data/examples/redis/namespaced.rb +2 -2
- data/lib/flipper/adapters/redis.rb +31 -19
- data/lib/flipper/version.rb +1 -1
- data/spec/flipper/adapters/redis_spec.rb +24 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f32d46dfcd16583c05e480c9ef51d79b938e5d56b2511a035917bd2964713846
|
4
|
+
data.tar.gz: 0557da15aa96cb415a03854ecb41e06c9ef3b873a4b1dfb86e3fe044c980c7d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b693aa4557dd1f61737e11c43c74982f2b629e3efddf9ddec55633f29c81715635b1ca8ca743e79facd5c30f3d104b774dcdd84e9381b9ed229a7509ddde2d32
|
7
|
+
data.tar.gz: be3676c53c6e42e5a25304605a3243e32a85d096681015217e4dbac822823f6642b122a551c9b3010abc79fdb664b1882f26b6e57295e16d333f7f670b64ec55
|
data/examples/redis/internals.rb
CHANGED
@@ -6,8 +6,8 @@ require 'flipper/adapters/redis'
|
|
6
6
|
client = Redis.new
|
7
7
|
|
8
8
|
# Register a few groups.
|
9
|
-
Flipper.register(:admins) { |
|
10
|
-
Flipper.register(:early_access) { |
|
9
|
+
Flipper.register(:admins) { |actor| actor.admin? }
|
10
|
+
Flipper.register(:early_access) { |actor| actor.early_access? }
|
11
11
|
|
12
12
|
# Create a user class that has flipper_id instance method.
|
13
13
|
User = Struct.new(:flipper_id)
|
@@ -12,8 +12,8 @@ adapter = Flipper::Adapters::Redis.new(namespaced_client)
|
|
12
12
|
flipper = Flipper.new(adapter)
|
13
13
|
|
14
14
|
# Register a few groups.
|
15
|
-
Flipper.register(:admins) { |
|
16
|
-
Flipper.register(:early_access) { |
|
15
|
+
Flipper.register(:admins) { |actor| actor.admin? }
|
16
|
+
Flipper.register(:early_access) { |actor| actor.early_access? }
|
17
17
|
|
18
18
|
# Create a user class that has flipper_id instance method.
|
19
19
|
User = Struct.new(:flipper_id)
|
@@ -7,18 +7,28 @@ module Flipper
|
|
7
7
|
class Redis
|
8
8
|
include ::Flipper::Adapter
|
9
9
|
|
10
|
-
# Private: The key that stores the set of known features.
|
11
|
-
FeaturesKey = :flipper_features
|
12
|
-
|
13
10
|
# Public: The name of the adapter.
|
14
11
|
attr_reader :name
|
15
12
|
|
13
|
+
attr_reader :key_prefix
|
14
|
+
|
15
|
+
def features_key
|
16
|
+
"#{key_prefix}flipper_features"
|
17
|
+
end
|
18
|
+
|
19
|
+
def key_for(feature_name)
|
20
|
+
"#{key_prefix}#{feature_name}"
|
21
|
+
end
|
22
|
+
|
16
23
|
# Public: Initializes a Redis flipper adapter.
|
17
24
|
#
|
18
|
-
# client - The Redis client to use.
|
19
|
-
|
25
|
+
# client - The Redis client to use.
|
26
|
+
# key_prefix - an optional prefix with which to namespace
|
27
|
+
# flipper's Redis keys
|
28
|
+
def initialize(client, key_prefix: nil)
|
20
29
|
@client = client
|
21
30
|
@name = :redis
|
31
|
+
@key_prefix = key_prefix
|
22
32
|
end
|
23
33
|
|
24
34
|
# Public: The set of known features.
|
@@ -29,9 +39,9 @@ module Flipper
|
|
29
39
|
# Public: Adds a feature to the set of known features.
|
30
40
|
def add(feature)
|
31
41
|
if redis_sadd_returns_boolean?
|
32
|
-
@client.sadd?
|
42
|
+
@client.sadd? features_key, feature.key
|
33
43
|
else
|
34
|
-
@client.sadd
|
44
|
+
@client.sadd features_key, feature.key
|
35
45
|
end
|
36
46
|
true
|
37
47
|
end
|
@@ -39,17 +49,17 @@ module Flipper
|
|
39
49
|
# Public: Removes a feature from the set of known features.
|
40
50
|
def remove(feature)
|
41
51
|
if redis_sadd_returns_boolean?
|
42
|
-
@client.srem?
|
52
|
+
@client.srem? features_key, feature.key
|
43
53
|
else
|
44
|
-
@client.srem
|
54
|
+
@client.srem features_key, feature.key
|
45
55
|
end
|
46
|
-
@client.del feature.key
|
56
|
+
@client.del key_for(feature.key)
|
47
57
|
true
|
48
58
|
end
|
49
59
|
|
50
60
|
# Public: Clears the gate values for a feature.
|
51
61
|
def clear(feature)
|
52
|
-
@client.del feature.key
|
62
|
+
@client.del key_for(feature.key)
|
53
63
|
true
|
54
64
|
end
|
55
65
|
|
@@ -78,14 +88,15 @@ module Flipper
|
|
78
88
|
#
|
79
89
|
# Returns true.
|
80
90
|
def enable(feature, gate, thing)
|
91
|
+
feature_key = key_for(feature.key)
|
81
92
|
case gate.data_type
|
82
93
|
when :boolean
|
83
94
|
clear(feature)
|
84
|
-
@client.hset
|
95
|
+
@client.hset feature_key, gate.key, thing.value.to_s
|
85
96
|
when :integer
|
86
|
-
@client.hset
|
97
|
+
@client.hset feature_key, gate.key, thing.value.to_s
|
87
98
|
when :set
|
88
|
-
@client.hset
|
99
|
+
@client.hset feature_key, to_field(gate, thing), 1
|
89
100
|
else
|
90
101
|
unsupported_data_type gate.data_type
|
91
102
|
end
|
@@ -101,13 +112,14 @@ module Flipper
|
|
101
112
|
#
|
102
113
|
# Returns true.
|
103
114
|
def disable(feature, gate, thing)
|
115
|
+
feature_key = key_for(feature.key)
|
104
116
|
case gate.data_type
|
105
117
|
when :boolean
|
106
|
-
@client.del
|
118
|
+
@client.del feature_key
|
107
119
|
when :integer
|
108
|
-
@client.hset
|
120
|
+
@client.hset feature_key, gate.key, thing.value.to_s
|
109
121
|
when :set
|
110
|
-
@client.hdel
|
122
|
+
@client.hdel feature_key, to_field(gate, thing)
|
111
123
|
else
|
112
124
|
unsupported_data_type gate.data_type
|
113
125
|
end
|
@@ -131,14 +143,14 @@ module Flipper
|
|
131
143
|
end
|
132
144
|
|
133
145
|
def read_feature_keys
|
134
|
-
@client.smembers(
|
146
|
+
@client.smembers(features_key).to_set
|
135
147
|
end
|
136
148
|
|
137
149
|
# Private: Gets a hash of fields => values for the given feature.
|
138
150
|
#
|
139
151
|
# Returns a Hash of fields => values.
|
140
152
|
def doc_for(feature, pipeline: @client)
|
141
|
-
pipeline.hgetall(feature.key)
|
153
|
+
pipeline.hgetall(key_for(feature.key))
|
142
154
|
end
|
143
155
|
|
144
156
|
def docs_for(features)
|
data/lib/flipper/version.rb
CHANGED
@@ -28,4 +28,28 @@ RSpec.describe Flipper::Adapters::Redis do
|
|
28
28
|
|
29
29
|
expect(Flipper.adapter.adapter).to be_a(Flipper::Adapters::Redis)
|
30
30
|
end
|
31
|
+
|
32
|
+
describe 'with a key_prefix' do
|
33
|
+
let(:subject) { described_class.new(client, key_prefix: "lockbox:") }
|
34
|
+
let(:feature) { Flipper::Feature.new(:search, subject) }
|
35
|
+
|
36
|
+
it_should_behave_like 'a flipper adapter'
|
37
|
+
|
38
|
+
it 'namespaces feature-keys' do
|
39
|
+
subject.add(feature)
|
40
|
+
|
41
|
+
expect(client.smembers("flipper_features")).to eq([])
|
42
|
+
expect(client.exists?("search")).to eq(false)
|
43
|
+
expect(client.smembers("lockbox:flipper_features")).to eq(["search"])
|
44
|
+
expect(client.hgetall("lockbox:search")).not_to eq(nil)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "can remove namespaced keys" do
|
48
|
+
subject.add(feature)
|
49
|
+
expect(client.smembers("lockbox:flipper_features")).to eq(["search"])
|
50
|
+
|
51
|
+
subject.remove(feature)
|
52
|
+
expect(client.smembers("lockbox:flipper_features")).to be_empty
|
53
|
+
end
|
54
|
+
end
|
31
55
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flipper-redis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.28.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Nunemaker
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: flipper
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.28.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: 0.28.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: redis
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|