Class: Flipper::Adapters::Redis
- Inherits:
-
Object
- Object
- Flipper::Adapters::Redis
- Includes:
- Flipper::Adapter
- Defined in:
- lib/flipper/adapters/redis.rb
Constant Summary collapse
- FeaturesKey =
Private: The key that stores the set of known features.
:flipper_features
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Public: The name of the adapter.
Instance Method Summary collapse
-
#add(feature) ⇒ Object
Public: Adds a feature to the set of known features.
-
#clear(feature) ⇒ Object
Public: Clears the gate values for a feature.
-
#disable(feature, gate, thing) ⇒ Object
Public: Disables a gate for a given thing.
-
#doc_for(feature) ⇒ Object
Private: Gets a hash of fields => values for the given feature.
-
#enable(feature, gate, thing) ⇒ Object
Public: Enables a gate for a given thing.
-
#features ⇒ Object
Public: The set of known features.
-
#fields_to_gate_value(fields, gate) ⇒ Object
Private: Returns a set of values given an array of fields and a gate.
-
#get(feature) ⇒ Object
Public: Gets the values for all gates for a given feature.
-
#initialize(client) ⇒ Redis
constructor
Public: Initializes a Redis flipper adapter.
-
#remove(feature) ⇒ Object
Public: Removes a feature from the set of known features.
-
#to_field(gate, thing) ⇒ Object
Private: Converts gate and thing to hash key.
-
#unsupported_data_type(data_type) ⇒ Object
Private.
Constructor Details
#initialize(client) ⇒ Redis
Public: Initializes a Redis flipper adapter.
client - The Redis client to use. Feel free to namespace it.
19 20 21 22 |
# File 'lib/flipper/adapters/redis.rb', line 19 def initialize(client) @client = client @name = :redis end |
Instance Attribute Details
#name ⇒ Object (readonly)
Public: The name of the adapter.
14 15 16 |
# File 'lib/flipper/adapters/redis.rb', line 14 def name @name end |
Instance Method Details
#add(feature) ⇒ Object
Public: Adds a feature to the set of known features.
30 31 32 33 |
# File 'lib/flipper/adapters/redis.rb', line 30 def add(feature) @client.sadd FeaturesKey, feature.key true end |
#clear(feature) ⇒ Object
Public: Clears the gate values for a feature.
45 46 47 48 |
# File 'lib/flipper/adapters/redis.rb', line 45 def clear(feature) @client.del feature.key true end |
#disable(feature, gate, thing) ⇒ Object
Public: Disables a gate for a given thing.
feature - The Flipper::Feature for the gate. gate - The Flipper::Gate to disable. thing - The Flipper::Type being disabled for the gate.
Returns true.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/flipper/adapters/redis.rb', line 99 def disable(feature, gate, thing) case gate.data_type when :boolean @client.del feature.key when :integer @client.hset feature.key, gate.key, thing.value.to_s when :set @client.hdel feature.key, to_field(gate, thing) else unsupported_data_type gate.data_type end true end |
#doc_for(feature) ⇒ Object
Private: Gets a hash of fields => values for the given feature.
Returns a Hash of fields => values.
117 118 119 |
# File 'lib/flipper/adapters/redis.rb', line 117 def doc_for(feature) @client.hgetall(feature.key) end |
#enable(feature, gate, thing) ⇒ Object
Public: Enables a gate for a given thing.
feature - The Flipper::Feature for the gate. gate - The Flipper::Gate to disable. thing - The Flipper::Type being disabled for the gate.
Returns true.
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/flipper/adapters/redis.rb', line 79 def enable(feature, gate, thing) case gate.data_type when :boolean, :integer @client.hset feature.key, gate.key, thing.value.to_s when :set @client.hset feature.key, to_field(gate, thing), 1 else unsupported_data_type gate.data_type end true end |
#features ⇒ Object
Public: The set of known features.
25 26 27 |
# File 'lib/flipper/adapters/redis.rb', line 25 def features @client.smembers(FeaturesKey).to_set end |
#fields_to_gate_value(fields, gate) ⇒ Object
Private: Returns a set of values given an array of fields and a gate.
Returns a Set of the values enabled for the gate.
129 130 131 132 133 134 |
# File 'lib/flipper/adapters/redis.rb', line 129 def fields_to_gate_value(fields, gate) regex = /^#{Regexp.escape(gate.key.to_s)}\// keys = fields.grep(regex) values = keys.map { |key| key.split('/', 2).last } values.to_set end |
#get(feature) ⇒ Object
Public: Gets the values for all gates for a given feature.
Returns a Hash of Flipper::Gate#key => value.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/flipper/adapters/redis.rb', line 53 def get(feature) result = {} doc = doc_for(feature) fields = doc.keys feature.gates.each do |gate| result[gate.key] = case gate.data_type when :boolean, :integer doc[gate.key.to_s] when :set fields_to_gate_value fields, gate else unsupported_data_type gate.data_type end end result end |
#remove(feature) ⇒ Object
Public: Removes a feature from the set of known features.
36 37 38 39 40 41 42 |
# File 'lib/flipper/adapters/redis.rb', line 36 def remove(feature) @client.multi do @client.srem FeaturesKey, feature.key @client.del feature.key end true end |
#to_field(gate, thing) ⇒ Object
Private: Converts gate and thing to hash key.
122 123 124 |
# File 'lib/flipper/adapters/redis.rb', line 122 def to_field(gate, thing) "#{gate.key}/#{thing.value}" end |
#unsupported_data_type(data_type) ⇒ Object
Private
137 138 139 |
# File 'lib/flipper/adapters/redis.rb', line 137 def unsupported_data_type(data_type) raise "#{data_type} is not supported by this adapter" end |