Permalink
Browse files

Updates for compatibility with latest flipper adapter api.

  • Loading branch information...
1 parent dc0ffc1 commit 7c48d19e6ec2eca822604d1561622e98d1eb20f4 @jnunemaker committed Feb 15, 2013
Showing with 82 additions and 21 deletions.
  1. +82 −0 lib/flipper/adapters/redis.rb
  2. +0 −21 spec/flipper/redis_spec.rb
@@ -4,30 +4,112 @@
module Flipper
module Adapters
class Redis
+ FeaturesKey = :flipper_features
+
+ attr_reader :name
+
def initialize(client)
@client = client
+ @name = :redis
+ end
+
+ # Public
+ def get(feature)
+ result = {}
+
+ feature.gates.each do |gate|
+ result[gate] = case gate.data_type
+ when :boolean, :integer
+ read key(feature, gate)
+ when :set
+ set_members key(feature, gate)
+ else
+ unsupported_data_type(gate.data_type)
+ end
+ end
+
+ result
+ end
+
+ # Public
+ def enable(feature, gate, thing)
+ case gate.data_type
+ when :boolean, :integer
+ write key(feature, gate), thing.value.to_s
+ when :set
+ set_add key(feature, gate), thing.value.to_s
+ else
+ unsupported_data_type(gate.data_type)
+ end
+
+ true
+ end
+
+ # Public
+ def disable(feature, gate, thing)
+ case gate.data_type
+ when :boolean
+ feature.gates.each do |gate|
+ delete key(feature, gate)
+ end
+ when :integer
+ write key(feature, gate), thing.value.to_s
+ when :set
+ set_delete key(feature, gate), thing.value.to_s
+ else
+ unsupported_data_type(gate.data_type)
+ end
+
+ true
+ end
+
+ # Public: Adds a feature to the set of known features.
+ def add(feature)
+ set_add(FeaturesKey, feature.name.to_s)
+ true
+ end
+
+ # Public: The set of known features.
+ def features
+ set_members(FeaturesKey)
+ end
+
+ # Private
+ def key(feature, gate)
+ "#{feature.key}/#{gate.key}"
+ end
+
+ # Private
+ def unsupported_data_type(data_type)
+ raise "#{data_type} is not supported by this adapter"
end
+ # Private
def read(key)
@client.get key.to_s
end
+ # Private
def write(key, value)
@client.set key.to_s, value.to_s
end
+ # Private
def delete(key)
@client.del key.to_s
end
+ # Private
def set_add(key, value)
@client.sadd(key.to_s, value.to_s)
end
+ # Private
def set_delete(key, value)
@client.srem(key.to_s, value.to_s)
end
+ # Private
def set_members(key)
@client.smembers(key.to_s).to_set
end
View
@@ -11,26 +11,5 @@
client.flushdb
end
- def read_key(key)
- client.get key.to_s
- rescue RuntimeError => e
- if e.message =~ /wrong kind of value/
- client.smembers(key.to_s).to_set
- else
- raise
- end
- end
-
- def write_key(key, value)
- case value
- when Array, Set
- value.each do |member|
- client.sadd key.to_s, member.to_s
- end
- else
- client.set key.to_s, value.to_s
- end
- end
-
it_should_behave_like 'a flipper adapter'
end

0 comments on commit 7c48d19

Please sign in to comment.