class Flippant::Adapter::Redis
Constants
- DEFAULT_KEY
Attributes
client[R]
serializer[R]
set_key[R]
Public Class Methods
new(client: ::Redis.current, set_key: DEFAULT_KEY, serializer: Flippant.serializer)
click to toggle source
# File lib/flippant/adapters/redis.rb, line 15 def initialize(client: ::Redis.current, set_key: DEFAULT_KEY, serializer: Flippant.serializer) @client = client @serializer = serializer @set_key = set_key end
Public Instance Methods
add(feature)
click to toggle source
# File lib/flippant/adapters/redis.rb, line 27 def add(feature) client.sadd(set_key, feature) end
breakdown(actor = nil)
click to toggle source
# File lib/flippant/adapters/redis.rb, line 31 def breakdown(actor = nil) features(:all).each_with_object({}) do |fkey, memo| memo[fkey] = actor.nil? ? feature_rules(fkey) : enabled?(fkey, actor) end end
clear()
click to toggle source
# File lib/flippant/adapters/redis.rb, line 37 def clear client.smembers(set_key).each { |fkey| remove(fkey) } end
disable(feature, group, values = [])
click to toggle source
# File lib/flippant/adapters/redis.rb, line 41 def disable(feature, group, values = []) namespaced = namespace(feature) if values.any? change_values(namespaced, group) do |old| old - values end else client.hdel(namespaced, group) end maybe_cleanup(feature) end
enable(feature, group, values = [])
click to toggle source
# File lib/flippant/adapters/redis.rb, line 55 def enable(feature, group, values = []) add(feature) change_values(namespace(feature), group) do |old| (old | values).sort end end
enabled?(feature, actor, registered = Flippant.registered)
click to toggle source
# File lib/flippant/adapters/redis.rb, line 63 def enabled?(feature, actor, registered = Flippant.registered) client.hgetall(namespace(feature)).any? do |group, values| if (block = registered[group]) block.call(actor, serializer.load(values)) end end end
exists?(feature, group)
click to toggle source
# File lib/flippant/adapters/redis.rb, line 71 def exists?(feature, group) if group.nil? client.sismember(set_key, feature) else client.hexists(namespace(feature), group) end end
features(filter = :all)
click to toggle source
# File lib/flippant/adapters/redis.rb, line 79 def features(filter = :all) if filter == :all client.smembers(set_key).sort else features(:all).select do |fkey| client.hexists(namespace(fkey), filter) end end end
load(loaded)
click to toggle source
# File lib/flippant/adapters/redis.rb, line 89 def load(loaded) client.multi do loaded.each do |feature, rules| client.sadd(set_key, feature) rules.each do |group, values| client.hset(namespace(feature), group, serializer.dump(values)) end end end end
remove(feature)
click to toggle source
# File lib/flippant/adapters/redis.rb, line 101 def remove(feature) client.multi do client.srem(set_key, feature) client.del(namespace(feature)) end end
rename(old_feature, new_feature)
click to toggle source
# File lib/flippant/adapters/redis.rb, line 108 def rename(old_feature, new_feature) old_feature = old_feature new_feature = new_feature old_namespaced = namespace(old_feature) new_namespaced = namespace(new_feature) client.watch(old_namespaced, new_namespaced) do client.multi do client.srem(set_key, old_feature) client.sadd(set_key, new_feature) client.rename(old_namespaced, new_namespaced) end end end
setup()
click to toggle source
# File lib/flippant/adapters/redis.rb, line 23 def setup true end
Private Instance Methods
change_values(namespaced, group) { |old_values| ... }
click to toggle source
# File lib/flippant/adapters/redis.rb, line 147 def change_values(namespaced, group) client.watch(namespaced) do old_values = get_values(namespaced, group) new_values = yield(old_values) client.multi do client.hset(namespaced, group, serializer.dump(new_values)) end end end
feature_rules(feature)
click to toggle source
# File lib/flippant/adapters/redis.rb, line 125 def feature_rules(feature) namespaced = namespace(feature) client.hgetall(namespaced).each_with_object({}) do |(key, val), memo| memo[key] = serializer.load(val) end end
get_values(namespaced, group)
click to toggle source
# File lib/flippant/adapters/redis.rb, line 133 def get_values(namespaced, group) serializer.load(client.hget(namespaced, group)) || [] end
maybe_cleanup(feature)
click to toggle source
# File lib/flippant/adapters/redis.rb, line 137 def maybe_cleanup(feature) namespaced = namespace(feature) client.srem(set_key, feature) if client.hkeys(namespaced).empty? end
namespace(feature)
click to toggle source
# File lib/flippant/adapters/redis.rb, line 143 def namespace(feature) "#{set_key}-#{feature}" end