lib/bastet/base.rb in bastet-0.0.2 vs lib/bastet/base.rb in bastet-0.1.0
- old
+ new
@@ -1,49 +1,23 @@
+require 'singleton'
+
class Bastet::Base
- attr_accessor :redis
+ include Singleton
- def initialize redis
- @redis = redis
+ def activate feature, group
+ Bastet.redis.sadd("feature_#{feature}", group.name)
end
- def activate feature, target
- act :sadd, feature, target
+ def deactivate feature, group
+ Bastet.redis.srem("feature_#{feature}", group.name)
end
- def deactivate feature, target
- act :srem, feature, target
+ def active? feature, entity
+ group_names = Bastet.redis.smembers("feature_#{feature}")
+ groups = Bastet.groups.select { |group| group_names.include?(group.name) }
+ groups.any? { |group| group.criteria.call(entity) }
end
- def active? feature, target
- act :sismember, feature, target
+ def inactive? feature, entity
+ !active? feature, entity
end
-
- def inactive? feature, target
- !active? feature, target
- end
-
- private
-
- def act method, feature, target
- @redis.send(method, key(feature, target), val(target))
- end
-
- def key feature, target
- [].tap do |key|
- key << "feature"
- key << feature.to_s
- key << namespace(target)
- end.join(":")
- end
-
- def val target
- instance?(target) ? target.id : target.to_s
- end
-
- def namespace target
- instance?(target) ? "users" : "groups"
- end
-
- def instance? target
- target.respond_to?(:id)
- end
end