Class: CanDo
- Inherits:
-
Object
- Object
- CanDo
- Defined in:
- lib/can_do.rb
Overview
Flips your features based on either a redis key, a config/features.yml file or environment variables. Redis keys always take precedence over Environment variables and the settings in your YAML file.
Constant Summary collapse
- CONNECTION_POOL_SIZE =
ENV.fetch("CANDO_CONNECTION_POOL_SIZE", 5)
- CONNECTION_POOL =
ConnectionPool.new(size: CONNECTION_POOL_SIZE, timeout: 5) do Redis.new(url: ENV["CANDO_REDIS_URL"]) end
- THE_TRUTH =
/^(true|t|yes|y|1)$/i- DEFAULT_NAMESPACE =
"defaults".freeze
Class Method Summary collapse
Class Method Details
.feature?(feature) ⇒ Boolean
38 39 40 41 42 43 44 |
# File 'lib/can_do.rb', line 38 def feature?(feature) is_enabled = read(feature) # If no block is passed, return true or false return is_enabled unless block_given? # If a block is passed, return block or nil yield if is_enabled end |
.features ⇒ Object
65 66 67 68 69 70 |
# File 'lib/can_do.rb', line 65 def features keys = pool.with { |redis| redis.keys(redis_key("*")) } keys.map { |key| key.sub(redis_key(nil), "") } rescue Redis::CannotConnectError [] end |
.read(feature) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/can_do.rb', line 46 def read(feature) name = feature.to_s shared_feature = pool.with { |redis| redis.get(redis_key(name)) } fallback_value = fallback.fetch(name, false) return !!(shared_feature =~ THE_TRUTH) unless shared_feature.nil? write(name, fallback_value) fallback_value rescue Redis::CannotConnectError fallback_value end |
.write(name, val) ⇒ Object
59 60 61 62 63 |
# File 'lib/can_do.rb', line 59 def write(name, val) pool.with { |redis| redis.set(redis_key(name), val) } == "OK" rescue Redis::CannotConnectError false end |